QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#93772#6167. 染色图的联通性问题Renshey20 859ms180356kbC++231.9kb2023-04-02 10:34:522023-04-02 10:34:56

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-02 10:34:56]
  • 评测
  • 测评结果:20
  • 用时:859ms
  • 内存:180356kb
  • [2023-04-02 10:34:52]
  • 提交

answer

#include <bits/stdc++.h>
#define Getchar() p1 == p2 and (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++
char buf[1 << 21], *p1, *p2;
inline int read (void)
{
	int x = 0; char c = Getchar();
	while (c < '0' or c > '9') c = Getchar();
	while (c >= '0' and c <= '9') x = x * 10 + c - 48, c = Getchar();
	return x;
}
const int maxn = 1000000 + 10;
int n, m, col[maxn]; long long Ans, val[maxn], ans[maxn];
int tot, dfn[maxn], low[maxn], times, st[maxn], tp;
std::vector<int> e[maxn], g[maxn];
std::unordered_map<int, int> sum, now, cnt[maxn];
inline void tarjan (int u)
{
	dfn[u] = low[u] = ++times; st[++tp] = u; now[col[u]]++;
	for (int v: e[u])
		if (!dfn[v])
		{
			tarjan(v); low[u] = std::min(low[u], low[v]);
			if (low[v] >= dfn[u])
			{
				auto add = [&] (int u, int v) {g[u].push_back(v); g[v].push_back(u);} ;
				add(u, ++tot);
				do add(st[tp], tot); while (st[tp--] != v) ;
			}
		}
		else low[u] = std::min(low[u], dfn[v]);
}
inline void merge (int u, int v)
{
	ans[u] += val[v];
	if (cnt[u].size() < cnt[v].size()) std::swap(cnt[u], cnt[v]), val[u] = val[v];
	for (auto [c, w]: cnt[v]) val[u] += 1LL * w * (now[c] - 2 * cnt[u][c] - w), ans[u] -= 1LL * w * cnt[u][c], cnt[u][c] += w;
	std::unordered_map<int, int>().swap(cnt[v]);
}
inline void dfs (int u, int fr)
{
	if (u <= n) cnt[u][col[u]]++, ans[u] = val[u] = now[col[u]] - 1;
	for (int v: g[u]) if (v != fr) dfs(v, u), merge(u, v);
}
signed main ()
{
	tot = n = read(); m = read();
	for (int i = 1; i <= n; i++) col[i] = read();
	for (int i = 1, u, v; i <= m; i++) u = read(), v = read(), e[u].push_back(v), e[v].push_back(u);
	for (int i = 1; i <= n; i++) if (!dfn[i])
	{
		std::unordered_map<int, int>().swap(now); tarjan(i); dfs(i, 0);
		for (auto [c, w]: cnt[i]) Ans += 1LL * sum[c] * w, sum[c] += w;
	}
	for (int i = 1; i <= n; i++) printf("%lld\n", ans[i] + Ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

114 4191
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
59 46
6 103
2 80
5 80
1 34
1 15
8 34
4 100
30 104
8 44
8 76
21 ...

output:


result:


Test #2:

score: 0
Time Limit Exceeded

input:

1000 138966
110 156 136 6 16 313 383 173 95 189 119 143 202 152 18 277 83 6 196 516 65 131 5 6 70 30 97 192 581 12 1 46 58 136 638 62 9 283 15 63 9 37 199 35 7 154 111 17 230 341 150 261 191 2 104 14 138 265 1 18 105 47 25 2 49 210 435 91 372 375 42 5 61 136 1 89 217 205 37 97 96 310 2 114 292 132 6...

output:


result:


Test #3:

score: 0
Time Limit Exceeded

input:

485803 491610
4 2 5 1 1 1 1 2 1 1 1 1 1 93 9 6 5 2 1 1 1 1 1 2 1 1 1 1 1 1 29 1 1 11 3 1 38 1 37 2 4 1 4 1 88 5 1 1 1 3 1 1 16 239 1 1 3 58 6 2 4 10 99 4 1 3 36 2 1 12 6 10 1 1 1 11 1 245 1 1 1 1 3 26 9 1 1 1 4 18 3 21 199 2 1 1 15 1 3 73 2 1 1 3 1 1123 1 2 1 2 79 12 18 2 1 1 82 2 7 1 123 1 67 1 5 1...

output:


result:


Test #4:

score: 10
Accepted
time: 618ms
memory: 180356kb

input:

497602 455793
110 42 3 46 144 1 5 26 43 17 7 1 2 1 90 1 5 47 1 2 24 3 3 2 1 2 42 1 1 10 1 4 3 116 1 3 1 12 8 1 1 1 1 44 65 1 3 33 1 3 1 70 3 2 110 1 41 1 14 6 1 1 1 1 2 654 5 44 1 10 1 1 51 1 1 1 1 1 118 4 1 692 37 8 47 6074 153 18 179 20 7 1 8 70 5 86 4 12 1 1 1 14 1 15 1 1 3 5 12 1 3 21 261 39 9 1...

output:

10904417211
8292771032
7984597550
7861776264
7804878662
7769060556
7743403983
7728775469
7716551095
7706072924
7698506534
7690231431
7686033401
7681227786
7677014101
7675085295
7673208337
7667198692
7668760494
7668677247
7668250976
7661594535
7661507280
7659525900
7660423570
7657549481
7657644534
76...

result:

ok 497602 numbers

Test #5:

score: 0
Time Limit Exceeded

input:

410863 494517
315605 167 272660 764 1193 76758 165361 315422 61913 359311 69074 75101 91529 41283 181704 199 136706 204701 237333 60064 228582 48986 124350 29453 12587 1269 54671 131785 283380 383 52825 53004 113549 18112 39356 304370 3768 26531 44468 195138 32392 273607 141118 18058 149582 253085 5...

output:


result:


Test #6:

score: 0
Time Limit Exceeded

input:

402237 497417
1 1 49 1 777 2 1328 39 1 17 3 77 3 286 6 1 4 1 1 1 1 149 1 11 6 100 1 1 5 3 1 1192 1 5 9 7 1 75 6 2 7 11 6 2 2 1 2 3751 2 157 1 5 1 41 61 65 1 1 126 10 6 9 1 1 1 4 1 22 1 1 1 4 5 1 53 1 136 11 1 1 1 1 2 1 2 58 1 1 2 1 1 7 4 3 1 1 7 68 1 2 30 2 103 10 1 37 1 88 168 1 18 1 1 2 58 20 1 27...

output:


result:


Test #7:

score: 0
Time Limit Exceeded

input:

393273 427651
2 1 243 4 1 9 7 1 1 1 1 81 1 3 18 1 3 1 5 1 1 1 1 1 1 1 38 2 1 1 4 90 5 1 9 1 2 1 38 1 4 42 1 1 121 334 9 5 39 1 1 2 1 2 1 1 1 2 15 1 20 15 1 1 9 1 1 404 38 4 1 1 1 44 48 4 1 2 2 3 665 2 1 1 3 1 7 1 1 2 1 1 2 1 1 124 1 6 12 1 2 1 149 4 1 1 26 10 1 4 1 19 1015 31 2 1 1 17 2 154 1 1 2 5 ...

output:


result:


Test #8:

score: 0
Time Limit Exceeded

input:

495491 499412
6 1 1 1 1 1 1 1 4 1 1 3 93 2 1 17 2 5 151 1 1 2 119 1 9 1 128 4 1 1 1 1 1 1 3 12 48 1 3 1 1 30 1 56 1 2 1 1 4 1 1 1 1 1 7 1 1 2 9 5 1 4 2 1 5 1 3 3 1 1 4 9 1 1 3 26 1 33 1 1 1 209 1 238 41 1 37 2 1 1 1 1 29 2 1 14 2 1 1 2 2 1 40 27 1 1 3 1 3 3 1 1 1 120 4 1 1 1 1 2 1 2 1 1 1 1 8 2 9 17...

output:


result:


Test #9:

score: 10
Accepted
time: 859ms
memory: 177408kb

input:

485728 479282
16 7 21 1 1423 303 4 746 46 767 553 1 156 268 1 169 1 2 4 6 2299 4 6 55 1500 242 2 1291 1 1 35 1 1 124 41 9 1 3 2 3 4 1 11 4 4 2 2 1 2 5 82 20 49 123 5 1 51 1 328 17 26 9 3 248 1 5 2995 5 6 5 10 147 5 1 8 1 1 71 1 1 43 3 3 7 100 53 2 69 44 3448 45 33 7 1 1 4 28 97 1 1 223 12 6 369 52 1...

output:

4750722741
4750843143
4750719716
4750835661
4750719719
4750719793
4750734038
4750755323
4750720572
4750719814
4750719743
4750719716
4750725640
4750719808
4750951605
4750719884
4750835661
4750878731
4750734038
4750728572
4750719717
4750734038
4750719716
4750720481
4750722081
4750719822
4750753812
475...

result:

ok 485728 numbers

Test #10:

score: 0
Time Limit Exceeded

input:

499268 499621
1008 256 6 5 150 15 286 3 1 2 1 1 1 5 4 45 1 1 13 1 1 164 1 8 1 2 12 4 1 4 76 5 4 18 1 1 1 24 4 4 1 1 1 1 1 3 2 21 6 1 1 1 1 166 5 1 1 3 54 3 1 5 2 1 1 4 6 1 24 1 990 2 1 2 14 10 4 1 1 19 1 10 1 1 614 1 56 2 188 1 12 6 2 1 2 100 1 1 543 6 28 18 1 1 1 1 5 1 51 163 2 6 915 21 1 2 3 3 1 4...

output:


result: