#include<iostream>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18;
int par[100010];
vector<int>gv[100010];
int sz[100010];
set<int>rev[100010];
map<int,int>cnt[100010];
ll ans;
vector<pi>univ;
inline void uni(int x,int y)
{
if(sz[x]>sz[y])
swap(x,y);
vector<int>putv;
ans-=(ll)cnt[x][y]*sz[y]+(ll)cnt[y][x]*sz[x];
ans+=(ll)sz[x]*sz[y]*2;
ans+=sz[x]*((ll)rev[y].size()-cnt[x][y]-cnt[y][y]);
for(int t:rev[x])
{
if(par[t]==x||par[t]==y)
continue;
if(rev[y].find(t)==rev[y].end())
putv.eb(t);
else
ans-=sz[x];
}
for(int&t:putv)
{
rev[y].ep(t);
if(cnt[y][par[t]]>0)
univ.eb(y,par[t]);
cnt[par[t]][y]++;
ans+=sz[y];
}
for(pi t:cnt[x])
{
if(t.fi==par[t.fi]&&t.se>0&&cnt[t.fi][y]>0)
univ.eb(y,t.fi);
if(t.fi==par[t.fi])
cnt[y][t.fi]+=t.se;
}
sz[y]+=sz[x];
for(int&t:gv[x])
par[t]=y,gv[y].eb(t);
gv[x].clear();
return;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n>>m;
for(int i=0;i++<n;)
gv[par[i]=i].eb(i),sz[i]=1;
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
if(par[u]!=par[v]&&rev[par[v]].find(u)==rev[par[v]].end())
{
ans+=sz[par[v]];
rev[par[v]].ep(u);
cnt[par[u]][par[v]]++;
if(cnt[par[v]][par[u]]>0)
{
univ.eb(u,v);
while(!univ.empty())
{
pi t=univ.back();
univ.pop_back();
if(par[t.fi]!=par[t.se])
uni(par[t.fi],par[t.se]);
}
}
}
cout<<ans<<'\n';
}
cout.flush();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
9 ms |
12032 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
9 ms |
12032 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
10 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
9 ms |
12032 KB |
Output is correct |
13 |
Correct |
8 ms |
12032 KB |
Output is correct |
14 |
Correct |
8 ms |
12032 KB |
Output is correct |
15 |
Correct |
10 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12032 KB |
Output is correct |
17 |
Correct |
8 ms |
12032 KB |
Output is correct |
18 |
Correct |
8 ms |
12136 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
9 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
9 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
25 |
Correct |
9 ms |
12160 KB |
Output is correct |
26 |
Correct |
8 ms |
12160 KB |
Output is correct |
27 |
Correct |
9 ms |
12032 KB |
Output is correct |
28 |
Correct |
8 ms |
12160 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12032 KB |
Output is correct |
31 |
Correct |
9 ms |
12160 KB |
Output is correct |
32 |
Correct |
9 ms |
12032 KB |
Output is correct |
33 |
Correct |
10 ms |
12160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
9 ms |
12032 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
9 ms |
12032 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
10 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
9 ms |
12032 KB |
Output is correct |
13 |
Correct |
8 ms |
12032 KB |
Output is correct |
14 |
Correct |
8 ms |
12032 KB |
Output is correct |
15 |
Correct |
10 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12032 KB |
Output is correct |
17 |
Correct |
8 ms |
12032 KB |
Output is correct |
18 |
Correct |
8 ms |
12136 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
9 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
9 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
25 |
Correct |
9 ms |
12160 KB |
Output is correct |
26 |
Correct |
8 ms |
12160 KB |
Output is correct |
27 |
Correct |
9 ms |
12032 KB |
Output is correct |
28 |
Correct |
8 ms |
12160 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12032 KB |
Output is correct |
31 |
Correct |
9 ms |
12160 KB |
Output is correct |
32 |
Correct |
9 ms |
12032 KB |
Output is correct |
33 |
Correct |
10 ms |
12160 KB |
Output is correct |
34 |
Correct |
12 ms |
12196 KB |
Output is correct |
35 |
Correct |
93 ms |
18136 KB |
Output is correct |
36 |
Correct |
121 ms |
21112 KB |
Output is correct |
37 |
Correct |
127 ms |
21112 KB |
Output is correct |
38 |
Correct |
116 ms |
20728 KB |
Output is correct |
39 |
Correct |
12 ms |
12672 KB |
Output is correct |
40 |
Correct |
15 ms |
12928 KB |
Output is correct |
41 |
Correct |
15 ms |
12928 KB |
Output is correct |
42 |
Correct |
13 ms |
12672 KB |
Output is correct |
43 |
Correct |
12 ms |
12672 KB |
Output is correct |
44 |
Correct |
14 ms |
12672 KB |
Output is correct |
45 |
Correct |
15 ms |
12672 KB |
Output is correct |
46 |
Correct |
14 ms |
12672 KB |
Output is correct |
47 |
Correct |
16 ms |
12800 KB |
Output is correct |
48 |
Correct |
14 ms |
12800 KB |
Output is correct |
49 |
Correct |
22 ms |
13800 KB |
Output is correct |
50 |
Correct |
146 ms |
21384 KB |
Output is correct |
51 |
Correct |
20 ms |
13056 KB |
Output is correct |
52 |
Correct |
130 ms |
19576 KB |
Output is correct |
53 |
Correct |
26 ms |
13688 KB |
Output is correct |
54 |
Correct |
125 ms |
20284 KB |
Output is correct |
55 |
Correct |
20 ms |
13032 KB |
Output is correct |
56 |
Correct |
15 ms |
13088 KB |
Output is correct |
57 |
Correct |
14 ms |
13056 KB |
Output is correct |
58 |
Correct |
17 ms |
13056 KB |
Output is correct |
59 |
Correct |
12 ms |
12780 KB |
Output is correct |
60 |
Correct |
108 ms |
17828 KB |
Output is correct |
61 |
Correct |
18 ms |
12928 KB |
Output is correct |
62 |
Correct |
129 ms |
20604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
12032 KB |
Output is correct |
2 |
Correct |
9 ms |
12160 KB |
Output is correct |
3 |
Correct |
8 ms |
12032 KB |
Output is correct |
4 |
Correct |
9 ms |
12032 KB |
Output is correct |
5 |
Correct |
9 ms |
12032 KB |
Output is correct |
6 |
Correct |
9 ms |
12032 KB |
Output is correct |
7 |
Correct |
10 ms |
12160 KB |
Output is correct |
8 |
Correct |
9 ms |
12160 KB |
Output is correct |
9 |
Correct |
10 ms |
12160 KB |
Output is correct |
10 |
Correct |
8 ms |
12032 KB |
Output is correct |
11 |
Correct |
8 ms |
12032 KB |
Output is correct |
12 |
Correct |
9 ms |
12032 KB |
Output is correct |
13 |
Correct |
8 ms |
12032 KB |
Output is correct |
14 |
Correct |
8 ms |
12032 KB |
Output is correct |
15 |
Correct |
10 ms |
12032 KB |
Output is correct |
16 |
Correct |
8 ms |
12032 KB |
Output is correct |
17 |
Correct |
8 ms |
12032 KB |
Output is correct |
18 |
Correct |
8 ms |
12136 KB |
Output is correct |
19 |
Correct |
8 ms |
12160 KB |
Output is correct |
20 |
Correct |
8 ms |
12160 KB |
Output is correct |
21 |
Correct |
9 ms |
12160 KB |
Output is correct |
22 |
Correct |
8 ms |
12160 KB |
Output is correct |
23 |
Correct |
9 ms |
12160 KB |
Output is correct |
24 |
Correct |
9 ms |
12160 KB |
Output is correct |
25 |
Correct |
9 ms |
12160 KB |
Output is correct |
26 |
Correct |
8 ms |
12160 KB |
Output is correct |
27 |
Correct |
9 ms |
12032 KB |
Output is correct |
28 |
Correct |
8 ms |
12160 KB |
Output is correct |
29 |
Correct |
9 ms |
12160 KB |
Output is correct |
30 |
Correct |
8 ms |
12032 KB |
Output is correct |
31 |
Correct |
9 ms |
12160 KB |
Output is correct |
32 |
Correct |
9 ms |
12032 KB |
Output is correct |
33 |
Correct |
10 ms |
12160 KB |
Output is correct |
34 |
Correct |
12 ms |
12196 KB |
Output is correct |
35 |
Correct |
93 ms |
18136 KB |
Output is correct |
36 |
Correct |
121 ms |
21112 KB |
Output is correct |
37 |
Correct |
127 ms |
21112 KB |
Output is correct |
38 |
Correct |
116 ms |
20728 KB |
Output is correct |
39 |
Correct |
12 ms |
12672 KB |
Output is correct |
40 |
Correct |
15 ms |
12928 KB |
Output is correct |
41 |
Correct |
15 ms |
12928 KB |
Output is correct |
42 |
Correct |
13 ms |
12672 KB |
Output is correct |
43 |
Correct |
12 ms |
12672 KB |
Output is correct |
44 |
Correct |
14 ms |
12672 KB |
Output is correct |
45 |
Correct |
15 ms |
12672 KB |
Output is correct |
46 |
Correct |
14 ms |
12672 KB |
Output is correct |
47 |
Correct |
16 ms |
12800 KB |
Output is correct |
48 |
Correct |
14 ms |
12800 KB |
Output is correct |
49 |
Correct |
22 ms |
13800 KB |
Output is correct |
50 |
Correct |
146 ms |
21384 KB |
Output is correct |
51 |
Correct |
20 ms |
13056 KB |
Output is correct |
52 |
Correct |
130 ms |
19576 KB |
Output is correct |
53 |
Correct |
26 ms |
13688 KB |
Output is correct |
54 |
Correct |
125 ms |
20284 KB |
Output is correct |
55 |
Correct |
20 ms |
13032 KB |
Output is correct |
56 |
Correct |
15 ms |
13088 KB |
Output is correct |
57 |
Correct |
14 ms |
13056 KB |
Output is correct |
58 |
Correct |
17 ms |
13056 KB |
Output is correct |
59 |
Correct |
12 ms |
12780 KB |
Output is correct |
60 |
Correct |
108 ms |
17828 KB |
Output is correct |
61 |
Correct |
18 ms |
12928 KB |
Output is correct |
62 |
Correct |
129 ms |
20604 KB |
Output is correct |
63 |
Correct |
590 ms |
63736 KB |
Output is correct |
64 |
Correct |
589 ms |
63664 KB |
Output is correct |
65 |
Correct |
585 ms |
63864 KB |
Output is correct |
66 |
Correct |
385 ms |
39664 KB |
Output is correct |
67 |
Correct |
582 ms |
52720 KB |
Output is correct |
68 |
Correct |
398 ms |
39664 KB |
Output is correct |
69 |
Correct |
502 ms |
39760 KB |
Output is correct |
70 |
Correct |
408 ms |
39660 KB |
Output is correct |
71 |
Correct |
369 ms |
39704 KB |
Output is correct |
72 |
Correct |
603 ms |
50552 KB |
Output is correct |
73 |
Correct |
724 ms |
50888 KB |
Output is correct |
74 |
Correct |
1348 ms |
89520 KB |
Output is correct |
75 |
Correct |
735 ms |
48112 KB |
Output is correct |
76 |
Correct |
965 ms |
68016 KB |
Output is correct |
77 |
Correct |
968 ms |
68204 KB |
Output is correct |
78 |
Correct |
373 ms |
44408 KB |
Output is correct |
79 |
Correct |
610 ms |
52720 KB |
Output is correct |
80 |
Correct |
426 ms |
45480 KB |
Output is correct |
81 |
Correct |
576 ms |
52320 KB |
Output is correct |
82 |
Correct |
810 ms |
62580 KB |
Output is correct |
83 |
Correct |
1038 ms |
62484 KB |
Output is correct |
84 |
Correct |
871 ms |
62384 KB |
Output is correct |
85 |
Correct |
706 ms |
62448 KB |
Output is correct |
86 |
Correct |
367 ms |
45704 KB |
Output is correct |
87 |
Correct |
379 ms |
47728 KB |
Output is correct |
88 |
Correct |
636 ms |
51444 KB |
Output is correct |
89 |
Correct |
885 ms |
66176 KB |
Output is correct |