Submission #2162148


Source Code Expand

#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,a) FOR(i,0,a)

using namespace std;
typedef pair<int,bool> P;

const int MAX_N=1e5;

set<P> G[MAX_N];
bool used[MAX_N];

int main(){
	int N;
	scanf("%d",&N);
	REP(i,N-1){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;
		b--;
		G[a].insert(P(b,false));
		G[b].insert(P(a,false));
	}
	REP(i,N-1){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;
		b--;
		G[a].insert(P(b,true));
		G[b].insert(P(a,true));
	}
	int c=0;
	REP(i,N){
		int v=i,v2;
		while(1){
			v2=-1;
			if (used[v]) break;
			for(auto ite=G[v].begin();ite!=G[v].end();){
				auto ite2=ite;
				ite2++;
				if (ite2==G[v].end()) break;
				if (ite->first==ite2->first){
					v2=ite->first;
					if (G[v].size()<G[v2].size()){
						swap(v,v2);
					}
					break;
				}
				ite++;
				if (ite->second){
					ite++;
				}
			}
			if (v2!=-1){
				c++;
				G[v].erase(P(v2,false));
				G[v].erase(P(v2,true));
				G[v2].erase(P(v,false));
				G[v2].erase(P(v,true));
				for(auto ite2=G[v2].begin();ite2!=G[v2].end();ite2++){
					G[ite2->first].erase(P(v2,ite2->second));
					G[ite2->first].insert(P(v,ite2->second));
					G[v].insert(*ite2);
//					ite2=G[v2].erase(ite2);
				}
				used[v2]=true;
			}else{
				break;
			}
		}
	}
	if (c==N-1){
		printf("YES\n");
	}else{
		printf("NO\n");
	}
	return 0;
}

Submission Info

Submission Time
Task E - Blue and Red Tree
User addeight
Language C++14 (GCC 5.4.1)
Score 0
Code Size 1419 Byte
Status TLE
Exec Time 6305 ms
Memory 26148 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:16:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
                ^
./Main.cpp:19:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a,&b);
                       ^
./Main.cpp:27:23: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d",&a,&b);
                       ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1400
Status
AC × 3
AC × 35
TLE × 18
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in34.txt, in35.txt, in36.txt, in37.txt, in38.txt, in39.txt, in4.txt, in40.txt, in41.txt, in42.txt, in43.txt, in44.txt, in45.txt, in46.txt, in47.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt AC 3 ms 4992 KB
in10.txt AC 165 ms 23680 KB
in11.txt AC 1192 ms 25856 KB
in12.txt AC 548 ms 23808 KB
in13.txt AC 959 ms 23808 KB
in14.txt AC 995 ms 23808 KB
in15.txt AC 906 ms 23808 KB
in16.txt AC 786 ms 23808 KB
in17.txt AC 1026 ms 23808 KB
in18.txt AC 695 ms 23808 KB
in19.txt AC 813 ms 25856 KB
in2.txt AC 3 ms 4992 KB
in20.txt AC 996 ms 23808 KB
in21.txt AC 1023 ms 23808 KB
in22.txt AC 769 ms 23808 KB
in23.txt AC 1186 ms 23808 KB
in24.txt AC 1135 ms 23808 KB
in25.txt AC 942 ms 25856 KB
in26.txt AC 747 ms 23808 KB
in27.txt AC 1014 ms 23808 KB
in28.txt AC 648 ms 23808 KB
in29.txt AC 984 ms 23808 KB
in3.txt AC 3 ms 4992 KB
in30.txt AC 1172 ms 23808 KB
in31.txt TLE 6305 ms 24064 KB
in32.txt TLE 6304 ms 24064 KB
in33.txt TLE 6304 ms 24064 KB
in34.txt TLE 6305 ms 24064 KB
in35.txt TLE 6305 ms 24064 KB
in36.txt TLE 6305 ms 24064 KB
in37.txt TLE 6304 ms 24064 KB
in38.txt TLE 6304 ms 25984 KB
in39.txt TLE 6304 ms 26112 KB
in4.txt AC 185 ms 23680 KB
in40.txt TLE 6304 ms 26112 KB
in41.txt TLE 6304 ms 26112 KB
in42.txt TLE 6304 ms 26112 KB
in43.txt TLE 6304 ms 26112 KB
in44.txt TLE 6305 ms 24064 KB
in45.txt TLE 6305 ms 24064 KB
in46.txt TLE 6304 ms 26112 KB
in47.txt TLE 6304 ms 26148 KB
in5.txt AC 170 ms 23680 KB
in6.txt TLE 6304 ms 26112 KB
in7.txt AC 175 ms 23680 KB
in8.txt AC 172 ms 23680 KB
in9.txt AC 169 ms 23680 KB
sample1.txt AC 3 ms 4992 KB
sample2.txt AC 3 ms 4992 KB
sample3.txt AC 3 ms 4992 KB