結果

問題 No.556 仁義なきサルたち
ユーザー myantamyanta
提出日時 2017-08-11 23:30:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,012 bytes
コンパイル時間 443 ms
コンパイル使用メモリ 37,760 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-20 23:42:26
合計ジャッジ時間 1,143 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 WA -
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 1 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 WA -
testcase_16 AC 3 ms
5,376 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:42:30: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   42 |                         scanf("%d%d", &a, &b);
      |                         ~~~~~^~~~~~~~~~~~~~~~

ソースコード

diff #

#include<cstdio>
#include<vector>


using namespace std;


struct g_t
{
	int id;
	int boss;
	int number;
};


int get_boss(vector<g_t>&g, int i)
{
	if(g[i].boss==i) return i;
	return g[i].boss=get_boss(g, g[i].boss);
}


int main(void)
{
	int n, m;

	while(scanf("%d%d", &n, &m)==2)
	{
		vector<g_t> g(n+1);

		for(int i=1;i<=n;i++)
		{
			g[i].id=i;
			g[i].boss=i;
			g[i].number=1;
		}

		for(int i=0;i<m;i++)
		{
			int a, b, boss_a, boss_b;

			scanf("%d%d", &a, &b);
			boss_a=get_boss(g, a);
			boss_b=get_boss(g, b);

			bool a_win=true;
			if(g[boss_a].number<g[boss_b].number) a_win=false;
			if(g[boss_a].number==g[boss_b].number)
			{
				if(g[boss_a].id>g[boss_b].id) a_win=false;
			}

			if(a_win)
			{
				g[boss_a].number+=g[boss_b].number;
				g[boss_b].boss=boss_a;
				g[boss_b].number=0;
			}
			else
			{
				g[boss_b].number+=g[boss_a].number;
				g[boss_a].boss=boss_b;
				g[boss_a].number=0;
			}
		}

		for(int i=1;i<=n;i++)
		{
			printf("%d\n", get_boss(g, i));
		}
	}

	return 0;
}
0