// Ivan Carvalho
// Voltage - JOI SC 2014
#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
const int MAXN = 1e5 + 10;
const int MAXM = 2*1e5 + 10;
int e1[MAXM],e2[MAXM],parent[MAXN],connectParent[MAXN],weight[MAXN],N,M,ans;
int possible,last,oldW[MAXM],oldz[MAXM],oldParent[MAXM],oldConnection[MAXM],oldWeight[MAXM],oldPossible[MAXM];
int find(int x){
if(x == parent[x]) return x;
return find(parent[x]);
}
int parity(int x){
if(x == parent[x]) return 0;
return (connectParent[x] ^ parity(parent[x]));
}
void join(int x,int y){
int w = find(x), z = find(y),px = parity(x),py = parity(y);
if(weight[w] < weight[z]){
swap(w,z);
swap(px,py);
swap(x,y);
}
last++;
oldW[last] = w;
oldz[last] = z;
oldParent[last] = parent[z];
oldConnection[last] = connectParent[z];
oldWeight[last] = weight[w];
oldPossible[last] = possible;
if(w == z){
if(px == py) possible = 0;
return;
}
parent[z] = w;
weight[w] += weight[z];
if(px == py) connectParent[z] = 1;
else connectParent[z] = 0;
}
void undo(){
int w = oldW[last] , z = oldz[last];
weight[w] = oldWeight[last];
parent[z] = oldParent[last];
connectParent[z] = oldConnection[last];
possible = oldPossible[last];
last--;
}
void divide_and_conquer(int lo,int hi){
if(lo == hi){
if(possible){
if(find(e1[lo]) != find(e2[lo])) ans++;
else if(parity(e1[lo]) == parity(e2[lo])) ans++;
}
return;
}
int mid = (lo+hi)/2;
for(int i = mid + 1;i<=hi;i++){
join(e1[i],e2[i]);
}
divide_and_conquer(lo,mid);
for(int i = mid + 1;i<=hi;i++){
undo();
}
for(int i = lo;i<=mid;i++){
join(e1[i],e2[i]);
}
divide_and_conquer(mid+1,hi);
for(int i = lo;i<=mid;i++){
undo();
}
}
int main(){
scanf("%d %d",&N,&M);
for(int i = 1;i<=N;i++){
parent[i] = i;
weight[i] = 1;
connectParent[i] = 0;
}
possible = 1;
for(int i = 1;i<=M;i++){
scanf("%d %d",&e1[i],&e2[i]);
}
divide_and_conquer(1,M);
printf("%d\n",ans);
return 0;
}
Compilation message
voltage.cpp: In function 'int main()':
voltage.cpp:99:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
99 | scanf("%d %d",&N,&M);
| ~~~~~^~~~~~~~~~~~~~~
voltage.cpp:110:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
110 | scanf("%d %d",&e1[i],&e2[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
512 KB |
Output is correct |
6 |
Correct |
2 ms |
512 KB |
Output is correct |
7 |
Correct |
2 ms |
512 KB |
Output is correct |
8 |
Correct |
2 ms |
512 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
512 KB |
Output is correct |
11 |
Correct |
2 ms |
512 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
5880 KB |
Output is correct |
2 |
Correct |
195 ms |
5904 KB |
Output is correct |
3 |
Correct |
83 ms |
5884 KB |
Output is correct |
4 |
Correct |
186 ms |
5884 KB |
Output is correct |
5 |
Correct |
13 ms |
896 KB |
Output is correct |
6 |
Correct |
179 ms |
5752 KB |
Output is correct |
7 |
Correct |
200 ms |
5880 KB |
Output is correct |
8 |
Correct |
200 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
5368 KB |
Output is correct |
2 |
Correct |
58 ms |
5760 KB |
Output is correct |
3 |
Correct |
57 ms |
5752 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
142 ms |
4728 KB |
Output is correct |
6 |
Correct |
137 ms |
5752 KB |
Output is correct |
7 |
Correct |
189 ms |
5880 KB |
Output is correct |
8 |
Correct |
180 ms |
5752 KB |
Output is correct |
9 |
Correct |
189 ms |
5760 KB |
Output is correct |
10 |
Correct |
184 ms |
5752 KB |
Output is correct |
11 |
Correct |
133 ms |
5752 KB |
Output is correct |
12 |
Correct |
166 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
9336 KB |
Output is correct |
2 |
Correct |
124 ms |
10120 KB |
Output is correct |
3 |
Correct |
1 ms |
1536 KB |
Output is correct |
4 |
Correct |
194 ms |
6136 KB |
Output is correct |
5 |
Correct |
207 ms |
6268 KB |
Output is correct |
6 |
Correct |
183 ms |
6264 KB |
Output is correct |
7 |
Correct |
335 ms |
10104 KB |
Output is correct |
8 |
Correct |
309 ms |
10104 KB |
Output is correct |
9 |
Correct |
302 ms |
10232 KB |
Output is correct |
10 |
Correct |
291 ms |
10104 KB |
Output is correct |
11 |
Correct |
230 ms |
10232 KB |
Output is correct |
12 |
Correct |
297 ms |
10232 KB |
Output is correct |
13 |
Correct |
255 ms |
10104 KB |
Output is correct |
14 |
Correct |
364 ms |
10232 KB |
Output is correct |
15 |
Correct |
293 ms |
10232 KB |
Output is correct |
16 |
Correct |
281 ms |
9976 KB |
Output is correct |
17 |
Correct |
319 ms |
10192 KB |
Output is correct |
18 |
Correct |
244 ms |
10108 KB |
Output is correct |