#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#define PB push_back
#define X first
#define Y second
using namespace std;
const int N = 1e5 + 500;
typedef long long ll;
typedef pair < int , int > pii;
vector < int > mene[N];
vector < pii > ja[N];
map < int, int > ima[N];
map < int, int > grp[N];
int par[N], n, m, oduz[N], siz[N];
int vel[N];
queue < int > spoji_X, spoji_Y;
int pr(int x){
if(x == par[x]) return x;
return par[x] = pr(par[x]);
}
ll sol = 0;
void spoji(int x,int y){
x = pr(x), y = pr(y);
if(x == y) return;
if((int)mene[x].size() + (int)ja[x].size() < (int)mene[y].size() + (int)ja[y].size())
swap(x, y);
int pres = 0;
for(int z : mene[y]){
if(pr(z) == x || pr(z) == y)
continue;
if(!ima[z][y]) continue;
ima[z][y] = 0;
grp[pr(z)][y]--;
if(ima[z][x]){
pres++;
}
else{
ima[z][x] = 1;
grp[pr(z)][x]++;
mene[x].PB(z);
}
if(grp[pr(z)][x] && grp[x][pr(z)]){
spoji_X.push(x); spoji_Y.push(pr(z));
}
}
map < pii , int > duplici;
for(pii tmp : ja[y]){
int z = tmp.Y, w = tmp.X;
if(pr(z) == x || pr(z) == y) continue;
if(duplici[{w, pr(z)}]) continue;
duplici[{w, pr(z)}] = 1;
grp[y][pr(z)]--;
grp[x][pr(z)]++;
ja[x].PB(tmp);
if(grp[pr(z)][x] && grp[x][pr(z)]){
spoji_X.push(x); spoji_Y.push(pr(z));
}
}
sol += (ll)(vel[y] - grp[x][y] - pres) * (ll)siz[x];
sol += (ll)(vel[x] - grp[y][x] - pres) * (ll)siz[y];
sol += 2LL * siz[x] * siz[y] - (ll)grp[y][x] * siz[x] - (ll)grp[x][y] * siz[y];
vel[x] = vel[x] + vel[y] - grp[x][y] - grp[y][x] - pres;
siz[x] = siz[x] + siz[y];
par[y] = x;
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 1;i <= n;i++)
par[i] = i, siz[i] = 1;
for(int j = 0;j < m;j++){
int x, y; scanf("%d%d", &x, &y);
if(ima[x][pr(y)] || pr(x) == pr(y)){
printf("%lld\n", sol);
continue;
}
if(!grp[pr(y)][pr(x)]){
mene[pr(y)].PB(x);
ima[x][pr(y)]++;
grp[pr(x)][pr(y)]++;
sol += siz[pr(y)];
vel[pr(y)]++;
printf("%lld\n", sol);
ja[pr(x)].PB({x, y});
continue;
}
x = pr(x), y = pr(y);
spoji(x, y);
for(;!spoji_X.empty();){
spoji(spoji_X.front(), spoji_Y.front());
spoji_X.pop(), spoji_Y.pop();
}
printf("%lld\n", sol);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:82:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
joitter2.cpp:86:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x, y; scanf("%d%d", &x, &y);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
14464 KB |
Output is correct |
2 |
Correct |
15 ms |
14464 KB |
Output is correct |
3 |
Correct |
14 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
15 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
16 ms |
14544 KB |
Output is correct |
8 |
Correct |
14 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14592 KB |
Output is correct |
10 |
Correct |
17 ms |
14464 KB |
Output is correct |
11 |
Correct |
15 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
15 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
15 ms |
14464 KB |
Output is correct |
16 |
Correct |
14 ms |
14464 KB |
Output is correct |
17 |
Correct |
16 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
15 ms |
14592 KB |
Output is correct |
21 |
Correct |
17 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14592 KB |
Output is correct |
24 |
Correct |
15 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14592 KB |
Output is correct |
26 |
Correct |
13 ms |
14644 KB |
Output is correct |
27 |
Correct |
12 ms |
14592 KB |
Output is correct |
28 |
Correct |
12 ms |
14592 KB |
Output is correct |
29 |
Correct |
12 ms |
14464 KB |
Output is correct |
30 |
Correct |
14 ms |
14440 KB |
Output is correct |
31 |
Correct |
14 ms |
14592 KB |
Output is correct |
32 |
Correct |
14 ms |
14592 KB |
Output is correct |
33 |
Correct |
18 ms |
14568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
14464 KB |
Output is correct |
2 |
Correct |
15 ms |
14464 KB |
Output is correct |
3 |
Correct |
14 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
15 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
16 ms |
14544 KB |
Output is correct |
8 |
Correct |
14 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14592 KB |
Output is correct |
10 |
Correct |
17 ms |
14464 KB |
Output is correct |
11 |
Correct |
15 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
15 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
15 ms |
14464 KB |
Output is correct |
16 |
Correct |
14 ms |
14464 KB |
Output is correct |
17 |
Correct |
16 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
15 ms |
14592 KB |
Output is correct |
21 |
Correct |
17 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14592 KB |
Output is correct |
24 |
Correct |
15 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14592 KB |
Output is correct |
26 |
Correct |
13 ms |
14644 KB |
Output is correct |
27 |
Correct |
12 ms |
14592 KB |
Output is correct |
28 |
Correct |
12 ms |
14592 KB |
Output is correct |
29 |
Correct |
12 ms |
14464 KB |
Output is correct |
30 |
Correct |
14 ms |
14440 KB |
Output is correct |
31 |
Correct |
14 ms |
14592 KB |
Output is correct |
32 |
Correct |
14 ms |
14592 KB |
Output is correct |
33 |
Correct |
18 ms |
14568 KB |
Output is correct |
34 |
Correct |
18 ms |
14720 KB |
Output is correct |
35 |
Correct |
199 ms |
20728 KB |
Output is correct |
36 |
Correct |
215 ms |
24032 KB |
Output is correct |
37 |
Correct |
242 ms |
24208 KB |
Output is correct |
38 |
Correct |
205 ms |
23724 KB |
Output is correct |
39 |
Correct |
20 ms |
15104 KB |
Output is correct |
40 |
Correct |
24 ms |
15232 KB |
Output is correct |
41 |
Correct |
25 ms |
15232 KB |
Output is correct |
42 |
Correct |
23 ms |
15104 KB |
Output is correct |
43 |
Correct |
19 ms |
15104 KB |
Output is correct |
44 |
Correct |
21 ms |
15104 KB |
Output is correct |
45 |
Correct |
22 ms |
15104 KB |
Output is correct |
46 |
Correct |
18 ms |
15104 KB |
Output is correct |
47 |
Correct |
19 ms |
15232 KB |
Output is correct |
48 |
Correct |
25 ms |
15200 KB |
Output is correct |
49 |
Correct |
29 ms |
16384 KB |
Output is correct |
50 |
Correct |
208 ms |
24312 KB |
Output is correct |
51 |
Correct |
23 ms |
15488 KB |
Output is correct |
52 |
Correct |
181 ms |
22112 KB |
Output is correct |
53 |
Correct |
28 ms |
16128 KB |
Output is correct |
54 |
Correct |
187 ms |
23136 KB |
Output is correct |
55 |
Correct |
21 ms |
15616 KB |
Output is correct |
56 |
Correct |
21 ms |
15616 KB |
Output is correct |
57 |
Correct |
21 ms |
15560 KB |
Output is correct |
58 |
Correct |
20 ms |
15616 KB |
Output is correct |
59 |
Correct |
19 ms |
15104 KB |
Output is correct |
60 |
Correct |
157 ms |
20064 KB |
Output is correct |
61 |
Correct |
19 ms |
15360 KB |
Output is correct |
62 |
Correct |
206 ms |
23576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
14464 KB |
Output is correct |
2 |
Correct |
15 ms |
14464 KB |
Output is correct |
3 |
Correct |
14 ms |
14464 KB |
Output is correct |
4 |
Correct |
13 ms |
14464 KB |
Output is correct |
5 |
Correct |
15 ms |
14464 KB |
Output is correct |
6 |
Correct |
13 ms |
14464 KB |
Output is correct |
7 |
Correct |
16 ms |
14544 KB |
Output is correct |
8 |
Correct |
14 ms |
14592 KB |
Output is correct |
9 |
Correct |
13 ms |
14592 KB |
Output is correct |
10 |
Correct |
17 ms |
14464 KB |
Output is correct |
11 |
Correct |
15 ms |
14464 KB |
Output is correct |
12 |
Correct |
14 ms |
14464 KB |
Output is correct |
13 |
Correct |
15 ms |
14464 KB |
Output is correct |
14 |
Correct |
13 ms |
14464 KB |
Output is correct |
15 |
Correct |
15 ms |
14464 KB |
Output is correct |
16 |
Correct |
14 ms |
14464 KB |
Output is correct |
17 |
Correct |
16 ms |
14464 KB |
Output is correct |
18 |
Correct |
13 ms |
14464 KB |
Output is correct |
19 |
Correct |
12 ms |
14464 KB |
Output is correct |
20 |
Correct |
15 ms |
14592 KB |
Output is correct |
21 |
Correct |
17 ms |
14592 KB |
Output is correct |
22 |
Correct |
13 ms |
14464 KB |
Output is correct |
23 |
Correct |
16 ms |
14592 KB |
Output is correct |
24 |
Correct |
15 ms |
14464 KB |
Output is correct |
25 |
Correct |
13 ms |
14592 KB |
Output is correct |
26 |
Correct |
13 ms |
14644 KB |
Output is correct |
27 |
Correct |
12 ms |
14592 KB |
Output is correct |
28 |
Correct |
12 ms |
14592 KB |
Output is correct |
29 |
Correct |
12 ms |
14464 KB |
Output is correct |
30 |
Correct |
14 ms |
14440 KB |
Output is correct |
31 |
Correct |
14 ms |
14592 KB |
Output is correct |
32 |
Correct |
14 ms |
14592 KB |
Output is correct |
33 |
Correct |
18 ms |
14568 KB |
Output is correct |
34 |
Correct |
18 ms |
14720 KB |
Output is correct |
35 |
Correct |
199 ms |
20728 KB |
Output is correct |
36 |
Correct |
215 ms |
24032 KB |
Output is correct |
37 |
Correct |
242 ms |
24208 KB |
Output is correct |
38 |
Correct |
205 ms |
23724 KB |
Output is correct |
39 |
Correct |
20 ms |
15104 KB |
Output is correct |
40 |
Correct |
24 ms |
15232 KB |
Output is correct |
41 |
Correct |
25 ms |
15232 KB |
Output is correct |
42 |
Correct |
23 ms |
15104 KB |
Output is correct |
43 |
Correct |
19 ms |
15104 KB |
Output is correct |
44 |
Correct |
21 ms |
15104 KB |
Output is correct |
45 |
Correct |
22 ms |
15104 KB |
Output is correct |
46 |
Correct |
18 ms |
15104 KB |
Output is correct |
47 |
Correct |
19 ms |
15232 KB |
Output is correct |
48 |
Correct |
25 ms |
15200 KB |
Output is correct |
49 |
Correct |
29 ms |
16384 KB |
Output is correct |
50 |
Correct |
208 ms |
24312 KB |
Output is correct |
51 |
Correct |
23 ms |
15488 KB |
Output is correct |
52 |
Correct |
181 ms |
22112 KB |
Output is correct |
53 |
Correct |
28 ms |
16128 KB |
Output is correct |
54 |
Correct |
187 ms |
23136 KB |
Output is correct |
55 |
Correct |
21 ms |
15616 KB |
Output is correct |
56 |
Correct |
21 ms |
15616 KB |
Output is correct |
57 |
Correct |
21 ms |
15560 KB |
Output is correct |
58 |
Correct |
20 ms |
15616 KB |
Output is correct |
59 |
Correct |
19 ms |
15104 KB |
Output is correct |
60 |
Correct |
157 ms |
20064 KB |
Output is correct |
61 |
Correct |
19 ms |
15360 KB |
Output is correct |
62 |
Correct |
206 ms |
23576 KB |
Output is correct |
63 |
Correct |
851 ms |
71904 KB |
Output is correct |
64 |
Correct |
837 ms |
71936 KB |
Output is correct |
65 |
Correct |
879 ms |
71824 KB |
Output is correct |
66 |
Correct |
415 ms |
42836 KB |
Output is correct |
67 |
Correct |
528 ms |
51312 KB |
Output is correct |
68 |
Correct |
439 ms |
42856 KB |
Output is correct |
69 |
Correct |
531 ms |
42472 KB |
Output is correct |
70 |
Correct |
420 ms |
42732 KB |
Output is correct |
71 |
Correct |
430 ms |
42860 KB |
Output is correct |
72 |
Correct |
618 ms |
49948 KB |
Output is correct |
73 |
Correct |
617 ms |
50144 KB |
Output is correct |
74 |
Correct |
1619 ms |
91468 KB |
Output is correct |
75 |
Correct |
884 ms |
52460 KB |
Output is correct |
76 |
Correct |
1139 ms |
71080 KB |
Output is correct |
77 |
Correct |
1125 ms |
71280 KB |
Output is correct |
78 |
Correct |
432 ms |
44652 KB |
Output is correct |
79 |
Correct |
670 ms |
53608 KB |
Output is correct |
80 |
Correct |
438 ms |
42564 KB |
Output is correct |
81 |
Correct |
652 ms |
49476 KB |
Output is correct |
82 |
Correct |
989 ms |
69736 KB |
Output is correct |
83 |
Correct |
1014 ms |
69712 KB |
Output is correct |
84 |
Correct |
812 ms |
69472 KB |
Output is correct |
85 |
Correct |
794 ms |
69540 KB |
Output is correct |
86 |
Correct |
319 ms |
43032 KB |
Output is correct |
87 |
Correct |
393 ms |
48020 KB |
Output is correct |
88 |
Correct |
622 ms |
50492 KB |
Output is correct |
89 |
Correct |
1105 ms |
69004 KB |
Output is correct |