#include<bits/stdc++.h>
using namespace std;
const int nmax=1e5+42;
int n,m;
int parent[nmax],SZ[nmax];
int root(int node)
{
if(node==parent[node])return node;
parent[node]=root(parent[node]);
return parent[node];
}
set< pair<int/*from*/,int/*to component*/> > all[nmax];
set< int > component[nmax];
set< pair<int/*from*/,int/*to*/> > edges_comp;
long long output=0;
int in[nmax];
long long score(int node)
{
node=root(node);
return 1LL*SZ[node]*(SZ[node]-1)/2*2+1LL*in[node]*SZ[node];
}
void add(int u,int v,bool special)
{
/*
cout<<"\n\n\n"<<endl;
cout<<"add "<<u<<" "<<v<<" "<<output<<" "<<special<<endl;
cout<<"edges_comp"<<endl;
for(auto k:edges_comp)cout<<k.first<<" "<<k.second<<endl;
for(int i=1;i<=n;i++)
{
cout<<i<<" -> "<<in[i]<<" "<<root(i)<<" "<<SZ[root(i)]<<endl;
cout<<"all: "<<endl;
for(auto j:all[i])cout<<j.first<<" "<<j.second<<endl;
cout<<"component: ";for(auto k:component[i])cout<<k<<" ";cout<<endl;
cout<<"---"<<endl;
}
*/
int u_root=root(u);
int v_root=root(v);
if(u_root==v_root)return;
if(special==0&&all[u_root].count({u,v_root}))return;
if(edges_comp.count({v_root,u_root})==0)//do not merge
{
if(all[u_root].count({u,v_root})==0)
{
output+=SZ[v_root];
in[v_root]++;
}
all[u_root].insert({u,v_root});
all[v_root].insert({u,v_root});
edges_comp.insert({u_root,v_root});
return;
}
if(SZ[u_root]<SZ[v_root])
{
swap(u_root,v_root);
swap(u,v);
}
edges_comp.erase({u_root,v_root});
edges_comp.erase({v_root,u_root});
//cout<<u_root<<" "<<u<<" "<<v_root<<" "<<v<<endl;
//merge
output=output-score(u_root)-score(v_root);
//cout<<"after sub "<<output<<endl;
set< pair<int/*from*/,int/*to component*/> > mem_all=all[v_root];
for(auto p:mem_all)
{
int where=-1;
pair<int,int> k=p;
//cout<<"moving "<<k.first<<" "<<k.second<<endl;
if(k.second==v_root&&component[u_root].count(k.first))
{
all[u_root].erase({k.first,v_root});
all[v_root].erase({k.first,v_root});
in[v_root]--;
where=1;
}
else if(k.second==v_root)
{
all[root(k.first)].erase(k);
k.second=u_root;
if(all[u_root].count(k)==0)in[u_root]++;
all[u_root].insert(k);
all[root(k.first)].insert(k);
edges_comp.insert({root(k.first),u_root});
edges_comp.erase({root(k.first),v_root});
where=2;
}
else if(component[u_root].count(k.second)==0)
{
//if(all[u_root].count({u_root,root(k.second)})==0)output+=SZ[u_root];in[root(k.second)]++;
all[u_root].insert({k.first,root(k.second)});
all[root(k.second)].insert({k.first,root(k.second)});
edges_comp.insert({u_root,root(k.second)});
/*
cout<<"Wrong "<<endl;
system("pause");
*/
where=3;
}
else
{
in[u_root]--;
all[u_root].erase({k.first,u_root});
all[v_root].erase({k.first,u_root});
where=4;
}
//cout<<"where= "<<where<<" "<<in[u_root]<<endl;
}
all[v_root]={};
in[v_root]=0;
SZ[u_root]+=SZ[v_root];
parent[v_root]=u_root;
output+=score(u_root);
//cout<<"after add "<<output<<endl;
for(auto k:component[v_root])
component[u_root].insert(k);
component[v_root]={};
for(auto k:mem_all)
add(k.first,k.second,1);
}
int main()
{
scanf("%i%i",&n,&m);
for(int i=1;i<=n;i++)parent[i]=i,SZ[i]=1,component[i]={i};
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%i%i",&u,&v);
add(u,v,0);
printf("%lld\n",output);
}
return 0;
}
Compilation message
joitter2.cpp: In function 'void add(int, int, bool)':
joitter2.cpp:92:13: warning: variable 'where' set but not used [-Wunused-but-set-variable]
92 | int where=-1;
| ^~~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:172:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
172 | scanf("%i%i",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joitter2.cpp:180:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
180 | scanf("%i%i",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
8 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9760 KB |
Output is correct |
21 |
Correct |
8 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
8 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
8 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
8 ms |
9728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
8 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9760 KB |
Output is correct |
21 |
Correct |
8 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
8 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
8 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
8 ms |
9728 KB |
Output is correct |
34 |
Correct |
11 ms |
9856 KB |
Output is correct |
35 |
Correct |
137 ms |
13908 KB |
Output is correct |
36 |
Correct |
190 ms |
16248 KB |
Output is correct |
37 |
Correct |
212 ms |
16248 KB |
Output is correct |
38 |
Correct |
189 ms |
15740 KB |
Output is correct |
39 |
Correct |
12 ms |
9856 KB |
Output is correct |
40 |
Correct |
14 ms |
9984 KB |
Output is correct |
41 |
Correct |
14 ms |
9984 KB |
Output is correct |
42 |
Correct |
10 ms |
9856 KB |
Output is correct |
43 |
Correct |
12 ms |
9984 KB |
Output is correct |
44 |
Correct |
12 ms |
9984 KB |
Output is correct |
45 |
Correct |
12 ms |
9856 KB |
Output is correct |
46 |
Correct |
10 ms |
9888 KB |
Output is correct |
47 |
Correct |
14 ms |
9984 KB |
Output is correct |
48 |
Correct |
14 ms |
9984 KB |
Output is correct |
49 |
Correct |
31 ms |
10872 KB |
Output is correct |
50 |
Correct |
205 ms |
16164 KB |
Output is correct |
51 |
Correct |
20 ms |
10240 KB |
Output is correct |
52 |
Correct |
152 ms |
14456 KB |
Output is correct |
53 |
Correct |
28 ms |
10752 KB |
Output is correct |
54 |
Correct |
168 ms |
15480 KB |
Output is correct |
55 |
Correct |
19 ms |
10368 KB |
Output is correct |
56 |
Correct |
19 ms |
10368 KB |
Output is correct |
57 |
Correct |
19 ms |
10880 KB |
Output is correct |
58 |
Correct |
22 ms |
10880 KB |
Output is correct |
59 |
Correct |
11 ms |
9984 KB |
Output is correct |
60 |
Correct |
124 ms |
12248 KB |
Output is correct |
61 |
Correct |
16 ms |
10112 KB |
Output is correct |
62 |
Correct |
177 ms |
15592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9728 KB |
Output is correct |
3 |
Correct |
7 ms |
9728 KB |
Output is correct |
4 |
Correct |
6 ms |
9728 KB |
Output is correct |
5 |
Correct |
7 ms |
9728 KB |
Output is correct |
6 |
Correct |
7 ms |
9728 KB |
Output is correct |
7 |
Correct |
8 ms |
9728 KB |
Output is correct |
8 |
Correct |
8 ms |
9728 KB |
Output is correct |
9 |
Correct |
8 ms |
9728 KB |
Output is correct |
10 |
Correct |
7 ms |
9728 KB |
Output is correct |
11 |
Correct |
7 ms |
9728 KB |
Output is correct |
12 |
Correct |
7 ms |
9728 KB |
Output is correct |
13 |
Correct |
7 ms |
9728 KB |
Output is correct |
14 |
Correct |
7 ms |
9728 KB |
Output is correct |
15 |
Correct |
7 ms |
9728 KB |
Output is correct |
16 |
Correct |
7 ms |
9728 KB |
Output is correct |
17 |
Correct |
7 ms |
9728 KB |
Output is correct |
18 |
Correct |
7 ms |
9728 KB |
Output is correct |
19 |
Correct |
7 ms |
9728 KB |
Output is correct |
20 |
Correct |
7 ms |
9760 KB |
Output is correct |
21 |
Correct |
8 ms |
9728 KB |
Output is correct |
22 |
Correct |
7 ms |
9728 KB |
Output is correct |
23 |
Correct |
7 ms |
9728 KB |
Output is correct |
24 |
Correct |
7 ms |
9728 KB |
Output is correct |
25 |
Correct |
8 ms |
9728 KB |
Output is correct |
26 |
Correct |
7 ms |
9728 KB |
Output is correct |
27 |
Correct |
7 ms |
9728 KB |
Output is correct |
28 |
Correct |
7 ms |
9728 KB |
Output is correct |
29 |
Correct |
7 ms |
9728 KB |
Output is correct |
30 |
Correct |
7 ms |
9728 KB |
Output is correct |
31 |
Correct |
8 ms |
9728 KB |
Output is correct |
32 |
Correct |
7 ms |
9728 KB |
Output is correct |
33 |
Correct |
8 ms |
9728 KB |
Output is correct |
34 |
Correct |
11 ms |
9856 KB |
Output is correct |
35 |
Correct |
137 ms |
13908 KB |
Output is correct |
36 |
Correct |
190 ms |
16248 KB |
Output is correct |
37 |
Correct |
212 ms |
16248 KB |
Output is correct |
38 |
Correct |
189 ms |
15740 KB |
Output is correct |
39 |
Correct |
12 ms |
9856 KB |
Output is correct |
40 |
Correct |
14 ms |
9984 KB |
Output is correct |
41 |
Correct |
14 ms |
9984 KB |
Output is correct |
42 |
Correct |
10 ms |
9856 KB |
Output is correct |
43 |
Correct |
12 ms |
9984 KB |
Output is correct |
44 |
Correct |
12 ms |
9984 KB |
Output is correct |
45 |
Correct |
12 ms |
9856 KB |
Output is correct |
46 |
Correct |
10 ms |
9888 KB |
Output is correct |
47 |
Correct |
14 ms |
9984 KB |
Output is correct |
48 |
Correct |
14 ms |
9984 KB |
Output is correct |
49 |
Correct |
31 ms |
10872 KB |
Output is correct |
50 |
Correct |
205 ms |
16164 KB |
Output is correct |
51 |
Correct |
20 ms |
10240 KB |
Output is correct |
52 |
Correct |
152 ms |
14456 KB |
Output is correct |
53 |
Correct |
28 ms |
10752 KB |
Output is correct |
54 |
Correct |
168 ms |
15480 KB |
Output is correct |
55 |
Correct |
19 ms |
10368 KB |
Output is correct |
56 |
Correct |
19 ms |
10368 KB |
Output is correct |
57 |
Correct |
19 ms |
10880 KB |
Output is correct |
58 |
Correct |
22 ms |
10880 KB |
Output is correct |
59 |
Correct |
11 ms |
9984 KB |
Output is correct |
60 |
Correct |
124 ms |
12248 KB |
Output is correct |
61 |
Correct |
16 ms |
10112 KB |
Output is correct |
62 |
Correct |
177 ms |
15592 KB |
Output is correct |
63 |
Correct |
1114 ms |
60140 KB |
Output is correct |
64 |
Correct |
1063 ms |
59896 KB |
Output is correct |
65 |
Correct |
1092 ms |
59896 KB |
Output is correct |
66 |
Correct |
277 ms |
19576 KB |
Output is correct |
67 |
Correct |
990 ms |
26696 KB |
Output is correct |
68 |
Correct |
270 ms |
20088 KB |
Output is correct |
69 |
Correct |
700 ms |
27192 KB |
Output is correct |
70 |
Correct |
280 ms |
20088 KB |
Output is correct |
71 |
Correct |
295 ms |
20088 KB |
Output is correct |
72 |
Correct |
954 ms |
27256 KB |
Output is correct |
73 |
Correct |
997 ms |
27060 KB |
Output is correct |
74 |
Correct |
2484 ms |
44996 KB |
Output is correct |
75 |
Correct |
1212 ms |
33272 KB |
Output is correct |
76 |
Correct |
2009 ms |
40952 KB |
Output is correct |
77 |
Correct |
1996 ms |
41128 KB |
Output is correct |
78 |
Correct |
505 ms |
25876 KB |
Output is correct |
79 |
Correct |
844 ms |
29440 KB |
Output is correct |
80 |
Correct |
393 ms |
27040 KB |
Output is correct |
81 |
Correct |
700 ms |
30368 KB |
Output is correct |
82 |
Correct |
2070 ms |
47548 KB |
Output is correct |
83 |
Correct |
1935 ms |
47448 KB |
Output is correct |
84 |
Correct |
1661 ms |
69060 KB |
Output is correct |
85 |
Correct |
1591 ms |
69056 KB |
Output is correct |
86 |
Correct |
384 ms |
20856 KB |
Output is correct |
87 |
Correct |
442 ms |
23012 KB |
Output is correct |
88 |
Correct |
1010 ms |
26992 KB |
Output is correct |
89 |
Correct |
1899 ms |
38788 KB |
Output is correct |