Submission #2168549


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];

typedef pair<int,int> edge;

int main(){
	stack<edge> st;
	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));
		st.push(edge(a,b));
	}
	int c=0;
	while(!st.empty()){
		edge e=st.top();
		st.pop();
		int from=e.first,to=e.second;
		auto ite=G[from].find(P(to,false));
		if (ite!=G[from].end()){
			auto ite2=ite;
			ite2++;
			if (ite2!=G[from].end() && ite2->first==to){
				if (G[from].size()<G[to].size()){
					swap(from,to);
				}
				c++;
				G[from].erase(P(to,false));
				G[from].erase(P(to,true));
				G[to].erase(P(from,false));
				G[to].erase(P(from,true));
				for(auto ite3=G[to].begin();ite3!=G[to].end();){
					st.push(edge(from,ite3->first));
					G[ite3->first].erase(P(to,ite3->second));
					G[ite3->first].insert(P(from,ite3->second));
					G[from].insert(*ite3);
					ite3=G[to].erase(ite3);
				}
			}
		}
	}
	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 1400
Code Size 1371 Byte
Status AC
Exec Time 393 ms
Memory 24448 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:18:16: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d",&N);
                ^
./Main.cpp:21: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:29: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 1400 / 1400
Status
AC × 3
AC × 53
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 144 ms 24448 KB
in11.txt AC 252 ms 24448 KB
in12.txt AC 244 ms 24448 KB
in13.txt AC 239 ms 24448 KB
in14.txt AC 304 ms 24448 KB
in15.txt AC 280 ms 24448 KB
in16.txt AC 288 ms 24448 KB
in17.txt AC 262 ms 24448 KB
in18.txt AC 285 ms 24448 KB
in19.txt AC 286 ms 24448 KB
in2.txt AC 3 ms 4992 KB
in20.txt AC 276 ms 24448 KB
in21.txt AC 253 ms 24448 KB
in22.txt AC 254 ms 24448 KB
in23.txt AC 248 ms 24448 KB
in24.txt AC 250 ms 24448 KB
in25.txt AC 246 ms 24448 KB
in26.txt AC 248 ms 24448 KB
in27.txt AC 247 ms 24448 KB
in28.txt AC 253 ms 24448 KB
in29.txt AC 252 ms 24448 KB
in3.txt AC 3 ms 4992 KB
in30.txt AC 253 ms 24448 KB
in31.txt AC 321 ms 24448 KB
in32.txt AC 334 ms 24448 KB
in33.txt AC 314 ms 24448 KB
in34.txt AC 327 ms 24448 KB
in35.txt AC 312 ms 24448 KB
in36.txt AC 316 ms 24448 KB
in37.txt AC 311 ms 24448 KB
in38.txt AC 307 ms 24448 KB
in39.txt AC 327 ms 24448 KB
in4.txt AC 141 ms 24448 KB
in40.txt AC 305 ms 24448 KB
in41.txt AC 318 ms 24448 KB
in42.txt AC 320 ms 24448 KB
in43.txt AC 320 ms 24448 KB
in44.txt AC 331 ms 24448 KB
in45.txt AC 303 ms 24448 KB
in46.txt AC 343 ms 24448 KB
in47.txt AC 393 ms 24448 KB
in5.txt AC 173 ms 24448 KB
in6.txt AC 352 ms 24448 KB
in7.txt AC 160 ms 24448 KB
in8.txt AC 180 ms 24448 KB
in9.txt AC 184 ms 24448 KB
sample1.txt AC 3 ms 4992 KB
sample2.txt AC 3 ms 4992 KB
sample3.txt AC 3 ms 4992 KB