#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const int MAXM = 3e5;
int N, M;
int par[MAXN+10], sz[MAXN+10];
set<int> in[MAXN+10], out[MAXN+10], point[MAXN+10], uf[MAXN+10];
ll ans;
int Find(int x) { return x==par[x] ? x : par[x]=Find(par[x]); }
void Union(int pu, int pv)
{
if(in[pv].size()+out[pv].size()+point[pv].size()+uf[pv].size()>in[pu].size()+out[pu].size()+point[pu].size()+uf[pu].size()) swap(pu, pv);
//printf("PU %d : ", pu); for(auto it : point[pu]) printf("%d ", it); printf("\n");
//printf("PV %d : ", pv); for(auto it : point[pv]) printf("%d ", it); printf("\n");
for(auto it : uf[pv]) if(!point[pu].count(it)) ans+=sz[pu];
//for(auto it : uf[pu]) if(!point[pv].count(it)) ans+=sz[pv];
//printf("HI %lld\n", ans);
ans+=(ll)sz[pu]*sz[pv];
for(auto it : point[pv]) if(uf[pu].count(it)) ans-=sz[pv];
ll t=point[pu].size();
for(auto it : uf[pv]) if(point[pu].count(it)) t--;
for(auto it : point[pv]) if(point[pu].count(it)) t--;
//printf("HI1 %lld\n", t);
ans+=t*sz[pv];
t=point[pv].size();
for(auto it : point[pv]) if(uf[pu].count(it)) t--;
for(auto it : point[pv]) if(point[pu].count(it)) t--;
//printf("HI2 %lld\n", t);
ans+=t*sz[pu];
vector<pii> V;
for(auto it : out[pv]) if(in[pu].count(it) && !in[pv].count(it)) V.push_back({it, pu});
for(auto it : in[pv]) if(!in[pu].count(it) && out[pu].count(it)) V.push_back({it, pu});
for(auto it : in[pv])
{
in[pu].insert(it);
out[it].erase(pv);
out[it].insert(pu);
}
for(auto it : out[pv])
{
out[pu].insert(it);
in[it].erase(pv);
in[it].insert(pu);
}
for(auto it : uf[pv]) uf[pu].insert(it), point[pu].erase(it);
for(auto it : point[pv]) if(!uf[pu].count(it)) point[pu].insert(it);
par[pv]=pu; sz[pu]+=sz[pv];
in[pv].clear(); out[pv].clear();
//in[pu].erase(pv); out[pu].erase(pv);
for(auto it : V) if(out[it.second].count(it.first)) Union(it.first, it.second);
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=N; i++) par[i]=i, sz[i]=1, uf[i].insert(i);
for(i=1; i<=M; i++)
{
int u, v;
scanf("%d%d", &u, &v);
int pu=Find(u), pv=Find(v);
if(pu!=pv && !point[pv].count(u))
{
out[pu].insert(pv);
in[pv].insert(pu);
point[pv].insert(u);
ans+=sz[pv];
if(out[pv].count(pu)) Union(pu, pv);
}
//for(j=1; j<=N; j++) printf("%d ", par[j]); printf("\n");
printf("%lld\n", ans);
}
}
Compilation message
joitter2.cpp: In function 'int main()':
joitter2.cpp:74:9: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
joitter2.cpp:76: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:82:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19072 KB |
Output is correct |
2 |
Correct |
15 ms |
19072 KB |
Output is correct |
3 |
Correct |
15 ms |
18944 KB |
Output is correct |
4 |
Correct |
15 ms |
19072 KB |
Output is correct |
5 |
Correct |
16 ms |
19072 KB |
Output is correct |
6 |
Correct |
15 ms |
19072 KB |
Output is correct |
7 |
Correct |
16 ms |
19200 KB |
Output is correct |
8 |
Correct |
20 ms |
19200 KB |
Output is correct |
9 |
Correct |
17 ms |
19200 KB |
Output is correct |
10 |
Correct |
16 ms |
19072 KB |
Output is correct |
11 |
Correct |
16 ms |
19072 KB |
Output is correct |
12 |
Correct |
15 ms |
19072 KB |
Output is correct |
13 |
Correct |
15 ms |
19072 KB |
Output is correct |
14 |
Correct |
17 ms |
19200 KB |
Output is correct |
15 |
Correct |
15 ms |
19072 KB |
Output is correct |
16 |
Correct |
16 ms |
19072 KB |
Output is correct |
17 |
Correct |
19 ms |
19120 KB |
Output is correct |
18 |
Correct |
17 ms |
19200 KB |
Output is correct |
19 |
Correct |
17 ms |
19200 KB |
Output is correct |
20 |
Correct |
16 ms |
19200 KB |
Output is correct |
21 |
Correct |
17 ms |
19200 KB |
Output is correct |
22 |
Correct |
16 ms |
19072 KB |
Output is correct |
23 |
Correct |
16 ms |
19200 KB |
Output is correct |
24 |
Correct |
16 ms |
19200 KB |
Output is correct |
25 |
Correct |
17 ms |
19200 KB |
Output is correct |
26 |
Correct |
16 ms |
19200 KB |
Output is correct |
27 |
Correct |
16 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
16 ms |
19072 KB |
Output is correct |
30 |
Correct |
16 ms |
19200 KB |
Output is correct |
31 |
Correct |
16 ms |
19072 KB |
Output is correct |
32 |
Correct |
16 ms |
19072 KB |
Output is correct |
33 |
Correct |
16 ms |
19200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19072 KB |
Output is correct |
2 |
Correct |
15 ms |
19072 KB |
Output is correct |
3 |
Correct |
15 ms |
18944 KB |
Output is correct |
4 |
Correct |
15 ms |
19072 KB |
Output is correct |
5 |
Correct |
16 ms |
19072 KB |
Output is correct |
6 |
Correct |
15 ms |
19072 KB |
Output is correct |
7 |
Correct |
16 ms |
19200 KB |
Output is correct |
8 |
Correct |
20 ms |
19200 KB |
Output is correct |
9 |
Correct |
17 ms |
19200 KB |
Output is correct |
10 |
Correct |
16 ms |
19072 KB |
Output is correct |
11 |
Correct |
16 ms |
19072 KB |
Output is correct |
12 |
Correct |
15 ms |
19072 KB |
Output is correct |
13 |
Correct |
15 ms |
19072 KB |
Output is correct |
14 |
Correct |
17 ms |
19200 KB |
Output is correct |
15 |
Correct |
15 ms |
19072 KB |
Output is correct |
16 |
Correct |
16 ms |
19072 KB |
Output is correct |
17 |
Correct |
19 ms |
19120 KB |
Output is correct |
18 |
Correct |
17 ms |
19200 KB |
Output is correct |
19 |
Correct |
17 ms |
19200 KB |
Output is correct |
20 |
Correct |
16 ms |
19200 KB |
Output is correct |
21 |
Correct |
17 ms |
19200 KB |
Output is correct |
22 |
Correct |
16 ms |
19072 KB |
Output is correct |
23 |
Correct |
16 ms |
19200 KB |
Output is correct |
24 |
Correct |
16 ms |
19200 KB |
Output is correct |
25 |
Correct |
17 ms |
19200 KB |
Output is correct |
26 |
Correct |
16 ms |
19200 KB |
Output is correct |
27 |
Correct |
16 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
16 ms |
19072 KB |
Output is correct |
30 |
Correct |
16 ms |
19200 KB |
Output is correct |
31 |
Correct |
16 ms |
19072 KB |
Output is correct |
32 |
Correct |
16 ms |
19072 KB |
Output is correct |
33 |
Correct |
16 ms |
19200 KB |
Output is correct |
34 |
Correct |
20 ms |
19200 KB |
Output is correct |
35 |
Correct |
144 ms |
22904 KB |
Output is correct |
36 |
Correct |
181 ms |
25464 KB |
Output is correct |
37 |
Correct |
182 ms |
25464 KB |
Output is correct |
38 |
Correct |
184 ms |
25208 KB |
Output is correct |
39 |
Correct |
20 ms |
19456 KB |
Output is correct |
40 |
Correct |
22 ms |
19712 KB |
Output is correct |
41 |
Correct |
22 ms |
19712 KB |
Output is correct |
42 |
Correct |
19 ms |
19456 KB |
Output is correct |
43 |
Correct |
21 ms |
19456 KB |
Output is correct |
44 |
Correct |
21 ms |
19584 KB |
Output is correct |
45 |
Correct |
20 ms |
19456 KB |
Output is correct |
46 |
Correct |
19 ms |
19456 KB |
Output is correct |
47 |
Correct |
22 ms |
19584 KB |
Output is correct |
48 |
Correct |
22 ms |
19584 KB |
Output is correct |
49 |
Correct |
34 ms |
20300 KB |
Output is correct |
50 |
Correct |
185 ms |
25700 KB |
Output is correct |
51 |
Correct |
27 ms |
19712 KB |
Output is correct |
52 |
Correct |
157 ms |
23932 KB |
Output is correct |
53 |
Correct |
32 ms |
20224 KB |
Output is correct |
54 |
Correct |
171 ms |
24860 KB |
Output is correct |
55 |
Correct |
25 ms |
19840 KB |
Output is correct |
56 |
Correct |
24 ms |
19840 KB |
Output is correct |
57 |
Correct |
26 ms |
20352 KB |
Output is correct |
58 |
Correct |
25 ms |
20352 KB |
Output is correct |
59 |
Correct |
22 ms |
19840 KB |
Output is correct |
60 |
Correct |
142 ms |
22452 KB |
Output is correct |
61 |
Correct |
24 ms |
19584 KB |
Output is correct |
62 |
Correct |
169 ms |
24952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
19072 KB |
Output is correct |
2 |
Correct |
15 ms |
19072 KB |
Output is correct |
3 |
Correct |
15 ms |
18944 KB |
Output is correct |
4 |
Correct |
15 ms |
19072 KB |
Output is correct |
5 |
Correct |
16 ms |
19072 KB |
Output is correct |
6 |
Correct |
15 ms |
19072 KB |
Output is correct |
7 |
Correct |
16 ms |
19200 KB |
Output is correct |
8 |
Correct |
20 ms |
19200 KB |
Output is correct |
9 |
Correct |
17 ms |
19200 KB |
Output is correct |
10 |
Correct |
16 ms |
19072 KB |
Output is correct |
11 |
Correct |
16 ms |
19072 KB |
Output is correct |
12 |
Correct |
15 ms |
19072 KB |
Output is correct |
13 |
Correct |
15 ms |
19072 KB |
Output is correct |
14 |
Correct |
17 ms |
19200 KB |
Output is correct |
15 |
Correct |
15 ms |
19072 KB |
Output is correct |
16 |
Correct |
16 ms |
19072 KB |
Output is correct |
17 |
Correct |
19 ms |
19120 KB |
Output is correct |
18 |
Correct |
17 ms |
19200 KB |
Output is correct |
19 |
Correct |
17 ms |
19200 KB |
Output is correct |
20 |
Correct |
16 ms |
19200 KB |
Output is correct |
21 |
Correct |
17 ms |
19200 KB |
Output is correct |
22 |
Correct |
16 ms |
19072 KB |
Output is correct |
23 |
Correct |
16 ms |
19200 KB |
Output is correct |
24 |
Correct |
16 ms |
19200 KB |
Output is correct |
25 |
Correct |
17 ms |
19200 KB |
Output is correct |
26 |
Correct |
16 ms |
19200 KB |
Output is correct |
27 |
Correct |
16 ms |
19200 KB |
Output is correct |
28 |
Correct |
15 ms |
19200 KB |
Output is correct |
29 |
Correct |
16 ms |
19072 KB |
Output is correct |
30 |
Correct |
16 ms |
19200 KB |
Output is correct |
31 |
Correct |
16 ms |
19072 KB |
Output is correct |
32 |
Correct |
16 ms |
19072 KB |
Output is correct |
33 |
Correct |
16 ms |
19200 KB |
Output is correct |
34 |
Correct |
20 ms |
19200 KB |
Output is correct |
35 |
Correct |
144 ms |
22904 KB |
Output is correct |
36 |
Correct |
181 ms |
25464 KB |
Output is correct |
37 |
Correct |
182 ms |
25464 KB |
Output is correct |
38 |
Correct |
184 ms |
25208 KB |
Output is correct |
39 |
Correct |
20 ms |
19456 KB |
Output is correct |
40 |
Correct |
22 ms |
19712 KB |
Output is correct |
41 |
Correct |
22 ms |
19712 KB |
Output is correct |
42 |
Correct |
19 ms |
19456 KB |
Output is correct |
43 |
Correct |
21 ms |
19456 KB |
Output is correct |
44 |
Correct |
21 ms |
19584 KB |
Output is correct |
45 |
Correct |
20 ms |
19456 KB |
Output is correct |
46 |
Correct |
19 ms |
19456 KB |
Output is correct |
47 |
Correct |
22 ms |
19584 KB |
Output is correct |
48 |
Correct |
22 ms |
19584 KB |
Output is correct |
49 |
Correct |
34 ms |
20300 KB |
Output is correct |
50 |
Correct |
185 ms |
25700 KB |
Output is correct |
51 |
Correct |
27 ms |
19712 KB |
Output is correct |
52 |
Correct |
157 ms |
23932 KB |
Output is correct |
53 |
Correct |
32 ms |
20224 KB |
Output is correct |
54 |
Correct |
171 ms |
24860 KB |
Output is correct |
55 |
Correct |
25 ms |
19840 KB |
Output is correct |
56 |
Correct |
24 ms |
19840 KB |
Output is correct |
57 |
Correct |
26 ms |
20352 KB |
Output is correct |
58 |
Correct |
25 ms |
20352 KB |
Output is correct |
59 |
Correct |
22 ms |
19840 KB |
Output is correct |
60 |
Correct |
142 ms |
22452 KB |
Output is correct |
61 |
Correct |
24 ms |
19584 KB |
Output is correct |
62 |
Correct |
169 ms |
24952 KB |
Output is correct |
63 |
Correct |
554 ms |
69112 KB |
Output is correct |
64 |
Correct |
556 ms |
68920 KB |
Output is correct |
65 |
Correct |
557 ms |
68856 KB |
Output is correct |
66 |
Correct |
340 ms |
36088 KB |
Output is correct |
67 |
Correct |
593 ms |
60024 KB |
Output is correct |
68 |
Correct |
312 ms |
36088 KB |
Output is correct |
69 |
Correct |
647 ms |
37240 KB |
Output is correct |
70 |
Correct |
374 ms |
36216 KB |
Output is correct |
71 |
Correct |
352 ms |
36088 KB |
Output is correct |
72 |
Correct |
685 ms |
44404 KB |
Output is correct |
73 |
Correct |
709 ms |
45944 KB |
Output is correct |
74 |
Correct |
1519 ms |
60664 KB |
Output is correct |
75 |
Correct |
948 ms |
41592 KB |
Output is correct |
76 |
Correct |
1215 ms |
51812 KB |
Output is correct |
77 |
Correct |
1214 ms |
51956 KB |
Output is correct |
78 |
Correct |
350 ms |
39032 KB |
Output is correct |
79 |
Correct |
580 ms |
41720 KB |
Output is correct |
80 |
Correct |
351 ms |
37880 KB |
Output is correct |
81 |
Correct |
641 ms |
39888 KB |
Output is correct |
82 |
Correct |
1296 ms |
54164 KB |
Output is correct |
83 |
Correct |
1246 ms |
54264 KB |
Output is correct |
84 |
Correct |
1198 ms |
79616 KB |
Output is correct |
85 |
Correct |
1218 ms |
79432 KB |
Output is correct |
86 |
Correct |
463 ms |
68984 KB |
Output is correct |
87 |
Correct |
509 ms |
69952 KB |
Output is correct |
88 |
Correct |
692 ms |
45116 KB |
Output is correct |
89 |
Correct |
1270 ms |
49508 KB |
Output is correct |