結果

問題 No.556 仁義なきサルたち
ユーザー myantamyanta
提出日時 2017-08-11 23:37:27
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,242 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 37,888 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 23:47:23
合計ジャッジ時間 1,002 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 4 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);

			if(boss_a==boss_b) continue;

			bool a_win=true;
//printf("%d(n=%d) vs %d(n=%d)\n", boss_a, g[boss_a].number, boss_b, g[boss_b].number);
			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));
		}
/*
		for(int i=1;i<=n;i++)
		{
			printf("id=%d  boss=%d  num=%d\n", g[i].id, g[i].boss, g[i].number);
		}
*/	}

	return 0;
}
0