#include<iostream>
#include<vector>
#include<algorithm>
const int N=2e5+2;
using namespace std;
vector<int> adj[N];
int cnt[N][3],color[N],level[N],numno=0,ans=0;
vector<pair<int,int> > lis;
bool used[N];
bool dfs(int x,bool valid){
bool scare=valid,cac;
for(int i=0;i<adj[x].size();i++){
if(!color[adj[x][i]]){
color[adj[x][i]]=3-color[x];
if(scare){
scare=dfs(adj[x][i],scare);
}
else{
cac=dfs(adj[x][i],scare);
}
}
else{
if(color[adj[x][i]]!=3-color[x]){
scare=false;
}
}
}
return scare;
}
void dfs1(int x,int p){
bool first=true;
for(int i=0;i<adj[x].size();i++){
if(!used[adj[x][i]]){
used[adj[x][i]]=true;
level[adj[x][i]]=level[x]+1;
dfs1(adj[x][i],x);
cnt[x][0]+=cnt[adj[x][i]][0];
cnt[x][1]+=cnt[adj[x][i]][1];
}
else{
if(adj[x][i]==p){
if(first){
first=false;
continue;
}
}
if(level[adj[x][i]]>level[x]){
continue;
}
if((level[x]-level[adj[x][i]])%2==0){
numno++;
cnt[x][0]++;
cnt[adj[x][i]][0]--;
}
else{
cnt[x][1]++;
cnt[adj[x][i]][1]--;
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n,m,i,j,k,root=0,cnt1=0;
cin>>n>>m;
for(i=1;i<=m;i++){
cin>>j>>k;
lis.push_back({min(j,k),max(j,k)});
adj[j].push_back(k);
adj[k].push_back(j);
}
sort(lis.begin(),lis.end());
for(i=1;i<lis.size();i++){
if(i>0&&lis[i].first==lis[i-1].first&&lis[i].second==lis[i-1].second){
cnt1++;
if(i==1){
cnt1++;
}
else{
if(lis[i-1].first!=lis[i-2].first||lis[i-1].second!=lis[i-2].second){
cnt1++;
}
}
}
}
bool cac;
for(i=1;i<=n;i++){
if(!color[i]){
color[i]=1;
cac=dfs(i,true);
if(!cac){
if(!root){
root=i;
}
else{
cout<<0;
return 0;
}
}
}
}
if(!root){
cout<<m-cnt1;
return 0;
}
used[root]=true;
dfs1(root,root);
if(numno==1){
ans++;
}
for(i=1;i<=n;i++){
if(i!=root&&used[i]&&cnt[i][0]==numno&&cnt[i][1]==0){
ans++;
}
}
cout<<ans;
}
Compilation message
voltage.cpp: In function 'bool dfs(int, bool)':
voltage.cpp:12:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].size();i++){
~^~~~~~~~~~~~~~
voltage.cpp:11:19: warning: variable 'cac' set but not used [-Wunused-but-set-variable]
bool scare=valid,cac;
^~~
voltage.cpp: In function 'void dfs1(int, int)':
voltage.cpp:32:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<adj[x].size();i++){
~^~~~~~~~~~~~~~
voltage.cpp: In function 'int main()':
voltage.cpp:74:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=1;i<lis.size();i++){
~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5116 KB |
Output is correct |
2 |
Correct |
6 ms |
5112 KB |
Output is correct |
3 |
Correct |
7 ms |
5112 KB |
Output is correct |
4 |
Correct |
7 ms |
5112 KB |
Output is correct |
5 |
Incorrect |
7 ms |
5112 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
9800 KB |
Output is correct |
2 |
Correct |
85 ms |
12136 KB |
Output is correct |
3 |
Correct |
75 ms |
10316 KB |
Output is correct |
4 |
Correct |
101 ms |
17228 KB |
Output is correct |
5 |
Correct |
13 ms |
6008 KB |
Output is correct |
6 |
Incorrect |
79 ms |
10856 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
9708 KB |
Output is correct |
2 |
Correct |
49 ms |
14024 KB |
Output is correct |
3 |
Correct |
56 ms |
14056 KB |
Output is correct |
4 |
Correct |
6 ms |
4984 KB |
Output is correct |
5 |
Correct |
71 ms |
13804 KB |
Output is correct |
6 |
Correct |
78 ms |
9452 KB |
Output is correct |
7 |
Incorrect |
85 ms |
11608 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
84 ms |
11464 KB |
Output is correct |
2 |
Correct |
97 ms |
14792 KB |
Output is correct |
3 |
Correct |
7 ms |
5368 KB |
Output is correct |
4 |
Correct |
98 ms |
15464 KB |
Output is correct |
5 |
Correct |
105 ms |
16764 KB |
Output is correct |
6 |
Correct |
105 ms |
15336 KB |
Output is correct |
7 |
Incorrect |
144 ms |
13284 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |