結果

問題 No.583 鉄道同好会
ユーザー bal4u
提出日時 2019-07-26 15:23:06
言語 C
(gcc 13.3.0)
結果
AC  
実行時間 5 ms / 2,000 ms
コード長 1,215 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 30,848 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-02 06:28:45
合計ジャッジ時間 1,514 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function 'in':
main.c:7:14: warning: implicit declaration of function 'getchar_unlocked' [-Wimplicit-function-declaration]
    7 | #define gc() getchar_unlocked()
      |              ^~~~~~~~~~~~~~~~
main.c:15:24: note: in expansion of macro 'gc'
   15 |         int n = 0, c = gc();
      |                        ^~
main.c: In function 'outs':
main.c:8:15: warning: implicit declaration of function 'putchar_unlocked' [-Wimplicit-function-declaration]
    8 | #define pc(c) putchar_unlocked(c)
      |               ^~~~~~~~~~~~~~~~
main.c:20:33: note: in expansion of macro 'pc'
   20 | void outs(char *s) { while (*s) pc(*s++); pc('\n'); }
      |                                 ^~

ソースコード

diff #

// yukicoder: No.583 鉄道同好会
// 2019.7.26 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void outs(char *s) { while (*s) pc(*s++); pc('\n'); }

#define MAX 505
/* UNION-FIND library */
int id[MAX], size[MAX];
void init(int n) { int i; for (i = 0; i < n; i++) id[i] = i, size[i] = 1; }
int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; }
int connected(int p, int q) { return root(p) == root(q); }
void unite(int p, int q) {
    int i = root(p), j = root(q); if (i == j) return;
    if (size[i] < size[j]) id[i] = j, size[j] += size[i]; else id[j] = i, size[i] += size[j];
}

int N, M;
char f[MAX];

int main()
{
	int i, x, p;

	N = in(), M = in();
	init(N);
	while (M--) {
		int a = in(), b = in();
		unite(a, b), f[a]++, f[b]++;
	}
	x = 0, p = -1;
	for (i = 0; i < N; i++) if (f[i]) {
		if (p < 0) p = root(i);
		else if (p != root(i)) { puts("NO"); return 0; }
		if (f[i] & 1) x++;
	}
	outs(x == 2 || x == 0? "YES": "NO"); 
	return 0;
}
0