AtCoder Grand Contest 014

Submission #1591124

Source codeソースコード

#include <bits/stdc++.h>
#define N 300010
using namespace std;

inline int read()
{
	int x=0,f=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}
	return x*f;
}
struct edge
{
	int v,next;
}vs[N<<1];
int n,st[N],ee,fa[N],size[N];
map <int,int> Ms[N];
inline int getfa(int x)
{
	return fa[x]==x? x: fa[x]=getfa(fa[x]);
}
int main()
{
	queue <int> q; 
	while(!q.empty()) q.pop();
	n=read(); for(int i=1;i<n+n-1;i++)
	{
		int u=read(), v=read();
		Ms[u][v]++; Ms[v][u]++;
		size[u]++; size[v]++;
		q.push(u); q.push(v);
	}
	for(int i=1;i<=n;i++) fa[i]=i;
	while(!q.empty())
	{
		int x=q.front(); q.pop();
		int y=q.front(); q.pop();
		x=getfa(x); y=getfa(y);
		if(Ms[x][y]<2) continue;
	//	cerr << x << " " << y << " " << endl;
		if(size[x]<size[y]) swap(x,y);
	//	cout << "merge:" << x << "  " << y << endl;
		for(map<int,int> :: iterator it=Ms[y].begin();it!=Ms[y].end();it++)
		{
			int d=(*it).second,z=(*it).first;
			Ms[y][z]=0; Ms[z][y]=0;
			if(z==x) continue;
			Ms[z][x]+=d; Ms[x][z]+=d;
			q.push(x); q.push(z);
			
		}
		/*for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=n;j++)
				printf("%d ",Ms[i][j]);
			printf("\n");
		}
		cout << endl;*/
		fa[y]=x;
	}
	//for(int i=1;i<=n;i++) cout << fa[i] << " "; cout << endl;
	for(int i=1;i<=n;i++) if(fa[1]!=fa[i]) {puts("NO"); return 0;}
	puts("YES");
	return 0;
}

Submission

Task問題 E - Blue and Red Tree
User nameユーザ名 wcz111
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 1400
Source lengthソースコード長 1459 Byte
File nameファイル名
Exec time実行時間 679 ms
Memory usageメモリ使用量 81408 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - sample1.txt,sample2.txt,sample3.txt
All 1400 / 1400 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

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
in1.txt AC 7 ms 18688 KB
in10.txt AC 115 ms 39040 KB
in11.txt AC 560 ms 68352 KB
in12.txt AC 588 ms 74112 KB
in13.txt AC 571 ms 72064 KB
in14.txt AC 632 ms 69632 KB
in15.txt AC 679 ms 75648 KB
in16.txt AC 567 ms 71296 KB
in17.txt AC 574 ms 72576 KB
in18.txt AC 588 ms 74240 KB
in19.txt AC 647 ms 81408 KB
in2.txt AC 7 ms 18688 KB
in20.txt AC 559 ms 70784 KB
in21.txt AC 617 ms 71552 KB
in22.txt AC 548 ms 67968 KB
in23.txt AC 524 ms 63104 KB
in24.txt AC 567 ms 65920 KB
in25.txt AC 591 ms 70656 KB
in26.txt AC 574 ms 69632 KB
in27.txt AC 544 ms 65152 KB
in28.txt AC 571 ms 69248 KB
in29.txt AC 559 ms 64512 KB
in3.txt AC 7 ms 18688 KB
in30.txt AC 544 ms 64512 KB
in31.txt AC 373 ms 50176 KB
in32.txt AC 370 ms 50176 KB
in33.txt AC 369 ms 50048 KB
in34.txt AC 398 ms 50176 KB
in35.txt AC 375 ms 51840 KB
in36.txt AC 368 ms 50048 KB
in37.txt AC 376 ms 50048 KB
in38.txt AC 357 ms 50048 KB
in39.txt AC 350 ms 50176 KB
in4.txt AC 118 ms 39040 KB
in40.txt AC 349 ms 50176 KB
in41.txt AC 369 ms 50176 KB
in42.txt AC 364 ms 50048 KB
in43.txt AC 368 ms 50432 KB
in44.txt AC 364 ms 50176 KB
in45.txt AC 359 ms 50048 KB
in46.txt AC 349 ms 50048 KB
in47.txt AC 355 ms 50176 KB
in5.txt AC 123 ms 39040 KB
in6.txt AC 349 ms 50176 KB
in7.txt AC 116 ms 39040 KB
in8.txt AC 120 ms 39040 KB
in9.txt AC 119 ms 39040 KB
sample1.txt AC 7 ms 18688 KB
sample2.txt AC 7 ms 18688 KB
sample3.txt AC 7 ms 18688 KB