1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| int n, m, a[1000006], b[1000006]; vector<vector<int>>v; int dep[1000006], fa[1000006], d1[1000006], d2[1000006], s1[1000006], s2[1000006]; int cnt; int del1, del2; void dfs(int k, int from) { rep(i, v[k].size()) { int x = v[k][i]; if (x == from)continue; if (dep[x]) { if (dep[x] < dep[k]) { if ((dep[k] - dep[x]) % 2 == 0)d1[x]--, d1[k]++, del1 = x, del2 = k, cnt++; else d2[x]--, d2[k]++; } continue; } dep[x] = dep[k] + 1; fa[x] = k; dfs(x, k); } } pair<int, int> dfs2(int k) { pair<int, int>tmp, sum; sum.fi = d1[k], sum.se = d2[k]; rep(i, v[k].size()) { int x = v[k][i]; if (fa[x] != k)continue; tmp = dfs2(x); sum.fi += tmp.fi, sum.se += tmp.se; } if (sum.fi == cnt && sum.se == 0)del1 = fa[k], del2 = k; return sum; } int ans[1000006]; void getans(int k) { rep(i, v[k].size()) { if (ans[v[k][i]] != -1)continue; if (k == del1 && v[k][i] == del2)continue; if (k == del2 && v[k][i] == del1)continue; ans[v[k][i]] = 1 - ans[k]; getans(v[k][i]); } } void solve() { cnt = 0; cin >> n >> m; v.resize(n); rep(i, n)v[i].clear(); rep(i, m) { cin >> a[i] >> b[i]; a[i]--, b[i]--; v[a[i]].pb(b[i]); v[b[i]].pb(a[i]); } rep(i, n)ans[i] = -1, dep[i] = d1[i] = d2[i] = 0; dep[0] = 1; del1 = del2 = -1; dfs(0, -1); if (cnt > 1) { del1 = del2 = -1; dfs2(0); if (del1 == -1) { cout << "NO\n"; return; } } ans[0] = 0; getans(0); rep(i, m)if (!ans[a[i]] && !ans[b[i]]) { rep(j, n)ans[j] = 1 - ans[j]; break; } cout << "YES\n"; rep(i, n)cout << ans[i]; cout << '\n'; }
|