染色
题目背景:
分析:结论题
这个结论题简直过于细节,而且因为卡掉算法的数据太难造,在下面拍几十万组都不一定WA得掉,但是交了就可能只有个10分左右了,大致说一下结论长相吧,首先如果存在奇环,显然是不行的,直接用两种颜色就可以卡掉了,然后我们可以发现,对于度数小于等于1的点,是并不影响的,只需要放和相连点不同的就行了,所以可以在一开始的时候直接缩掉,然后对于不同的连通块,显然是互不影响的,我们只需要讨论单个连通块,排除了上面情况后,剩下的就是一堆偶环随意嵌套连接了,考虑如果两个偶环只有1个公共点,我们可以找出公共点,让它所在的其中一个环上的相邻点的颜色集合为(B, C), (A, C),环上其他点为(A, B),另一个环上的相邻点为(A, B), (A, C),环上其他点为(B, C),然后这个公共点集合为(A, C),显然,第一个环上的相邻点中一定有一个为C,第二个环上的相邻点中一定有一个为A,这样就会凉掉,所以不存在只有一个公共点的两个偶环,那么现在剩下的情况就只有,一个大偶环,每一次选择相连点中没有的就可以了,两个度数为3的点,然后中间有三条不相交的路径,经验证这三天路径长度只能为2-2-偶数,直接判断一下就可以了.
综上就做完了,复杂度O(T(n + m))
Source:
/* created by scarlyw */ #include <cstdio> #include <string> #include <algorithm> #include <cstring> #include <iostream> #include <cmath> #include <cctype> #include <vector> #include <set> #include <queue> #include <ctime> #include <bitset> inline char read() { static const int IN_LEN = 1024 * 1024; static char buf[IN_LEN], *s, *t; if (s == t) { t = (s = buf) + fread(buf, 1, IN_LEN, stdin); if (s == t) return -1; } return *s++; } // /* template<class T> inline void R(T &x) { static char c; static bool iosig; for (c = read(), iosig = false; !isdigit(c); c = read()) { if (c == -1) return ; if (c == '-') iosig = true; } for (x = 0; isdigit(c); c = read()) x = ((x << 2) + x << 1) + (c ^ '0'); if (iosig) x = -x; } //*/ const int OUT_LEN = 1024 * 1024; char obuf[OUT_LEN]; char *oh = obuf; inline void write_char(char c) { if (oh == obuf + OUT_LEN) fwrite(obuf, 1, OUT_LEN, stdout), oh = obuf; *oh++ = c; } template<class T> inline void W(T x) { static int buf[30], cnt; if (x == 0) write_char('0'); else { if (x < 0) write_char('-'), x = -x; for (cnt = 0; x; x /= 10) buf[++cnt] = x % 10 + 48; while (cnt) write_char(buf[cnt--]); } } inline void flush() { fwrite(obuf, 1, oh - obuf, stdout), oh = obuf; } /* template<class T> inline void R(T &x) { static char c; static bool iosig; for (c = getchar(), iosig = false; !isdigit(c); c = getchar()) if (c == '-') iosig = true; for (x = 0; isdigit(c); c = getchar()) x = ((x << 2) + x << 1) + (c ^ '0'); if (iosig) x = -x; } //*/ inline void write(bool flag) { if (flag) write_char('Y'), write_char('E'), write_char('S'), write_char('\n'); else write_char('N'), write_char('O'), write_char('\n'); } const int MAXN = 20000 + 10; int t, n, m, x, y; int father[MAXN], d[MAXN]; std::vector<int> edge[MAXN], pos[MAXN]; std::queue<int> q; bool vis[MAXN]; inline void add_edge(int x, int y) { edge[x].push_back(y), edge[y].push_back(x), d[x]++, d[y]++; } inline int get_father(int x) { return (father[x] == x) ? (x) : (father[x] = get_father(father[x])); } inline void merge(int x, int y) { int fa1 = get_father(x), fa2 = get_father(y); if (fa1 != fa2) father[fa1] = fa2; } inline bool check() { for (int i = 1; i <= n; ++i) if (get_father(i) == i) { if (pos[i].size() == 1) continue ; int cnt3 = 0, x = 0, y = 0; for (int j = 0; j < pos[i].size(); ++j) { int cur = pos[i][j]; if (d[cur] > 3) return false; if (d[cur] == 3) cnt3++, (x == 0) ? (x = cur) : (y = cur); } if (cnt3 == 1 || cnt3 > 2) return false; if (cnt3 == 0 && pos[i].size() % 2) return false; if (cnt3 == 2) { int cnt_num = 0; if (pos[i].size() % 2 == 0) return false; for (int j = 0; j < pos[i].size(); ++j) { int cur = pos[i][j], cnt = 0; for (int p = 0; p < edge[cur].size(); ++p) if (edge[cur][p] == x || edge[cur][p] == y) cnt++; cnt_num += (cnt == 2); } if (cnt_num < 2) return false; } } return true; } inline void solve() { R(n), R(m); for (int i = 1; i <= n; ++i) edge[i].clear(), pos[i].clear(), d[i] = 0, vis[i] = false; for (int i = 1; i <= m; ++i) R(x), R(y), add_edge(x, y); for (int i = 1; i <= n; ++i) if (d[i] == 1) q.push(i), vis[i] = true; while (!q.empty()) { int cur = q.front(); q.pop(); for (int p = 0; p < edge[cur].size(); ++p) { int v = edge[cur][p]; if (!vis[v]) if (--d[v] == 1) q.push(v), vis[v] = true; } } for (int i = 1; i <= n; ++i) if (!vis[i]) father[i] = i; for (int i = 1; i <= n; ++i) if (!vis[i]) for (int p = 0; p < edge[i].size(); ++p) { int v = edge[i][p]; if (!vis[v]) merge(i, v); } for (int i = 1; i <= n; ++i) if (!vis[i]) pos[get_father(i)].push_back(i); write(check()); } int main() { //freopen("in.in", "r", stdin); R(t); while (t--) solve(); flush(); return 0; }