結果

問題 No.1420 国勢調査 (Easy)
ユーザー publflpublfl
提出日時 2021-03-05 22:51:25
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 66 ms / 2,000 ms
コード長 806 bytes
コンパイル時間 436 ms
コンパイル使用メモリ 50,176 KB
実行使用メモリ 11,488 KB
最終ジャッジ日時 2024-10-07 03:57:08
合計ジャッジ時間 3,823 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <vector>

std::vector< std::pair<int,int> > V[100010];
int check[100010];
void func(int k, int prev, int C)
{
	check[k] = C;
	for(int i=0;i<V[k].size();i++)
	{
		if(V[k][i].first==prev) continue;
		if(check[V[k][i].first]==-1) func(V[k][i].first,k,C^V[k][i].second);
	}
}
int main()
{
	int a,b;
	scanf("%d%d",&a,&b);
	for(int i=1;i<=b;i++)
	{
		int c,d,e;
		scanf("%d%d%d",&c,&d,&e);
		V[c].push_back(std::make_pair(d,e));
		V[d].push_back(std::make_pair(c,e));
	}
	
	for(int i=1;i<=a;i++) check[i] = -1;
	for(int i=1;i<=a;i++) if(check[i]==-1) func(i,0,0);
	
	for(int i=1;i<=a;i++)
	{
		for(int j=0;j<V[i].size();j++)
		{
			if((check[i]^check[V[i][j].first])!=V[i][j].second)
			{
				printf("-1");
				return 0;
			}
		}
	}
	
	for(int i=1;i<=a;i++) printf("%d\n",check[i]);
}
0