Bzoj4238

Solution

基本与 CF19E 相同。

值得注意的是,如果这个图为二分图,那么在偶环里边也不可以选。具体上的原因是题目的差异。

code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
#define maxn 2000005
#define ll long long
#define put() putchar('\n')
using namespace std;
inline void read(int &x){
int f=1;x=0;char c=getchar();
while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
while (c>='0'&&c<='9') {x=x*10+c-'0';c=getchar();}
x*=f;
}
int head=1,h[maxn];
struct yyy{
int to,z,flag;
inline void add(int x,int y) {
to=y;z=h[x];h[x]=head;flag=0;
}
}a[maxn*2];
int c[maxn],fa[maxn],cnt[2][maxn];
int n,m,tot,times,dfn[maxn];
inline void dfs(int x,int pre,int color) {
int i;c[x]=color;dfn[x]=++times;
for (i=h[x];i;i=a[i].z)
if ((i^1)!=fa[x]) {
if (!c[a[i].to]) fa[a[i].to]=i,a[i].flag=a[i^1].flag=1,dfs(a[i].to,x,3-color);
else if (dfn[a[i].to]<dfn[x]){
if (c[a[i].to]!=c[x]) cnt[0][fa[x]]++,cnt[0][fa[a[i].to]]--,cnt[0][i]++;
else cnt[1][fa[x]]++,cnt[1][fa[a[i].to]]--,cnt[1][i]++,tot++;
}
}
}
int vis[maxn];
inline void dfs2(int x,int pre) {
int i;vis[x]=1;
for (i=h[x];i;i=a[i].z)
if (a[i].to!=pre&&!vis[a[i].to]) {
dfs2(a[i].to,x);
cnt[0][fa[x]]+=cnt[0][i];
cnt[1][fa[x]]+=cnt[1][i];
}
}
inline bool dfs3(int x,int pre,int color) {
int i,tmp=0;c[x]=color;
for (i=h[x];i;i=a[i].z)
if (a[i].to!=pre&&a[i].flag==0) {
if (!c[a[i].to]) tmp|=dfs3(a[i].to,x,3-color);
else if (c[a[i].to]==c[x]) tmp=1;
}
return tmp;
}
inline void solve(void) {
int i,x,y,ans=0;
read(n);read(m);
head=1,tot=0;times=0;
for (i=1;i<=n;i++) h[i]=vis[i]=c[i]=fa[i]=dfn[i]=0;
for (i=1;i<=2*m+1;i++) cnt[0][i]=cnt[1][i]=0;
for (i=1;i<=m;i++) {
read(x);read(y);
a[++head].add(x,y);
a[++head].add(y,x);
}
for (i=1;i<=n;i++) if (!dfn[i]) dfs(i,0,1);
for (i=1;i<=n;i++) if (!vis[i]) dfs2(i,0);
for (i=1;i<=n;i++) c[i]=0;
int flag=0;
for (i=2;i<=head;i+=2) cnt[0][i]+=cnt[0][i^1],cnt[1][i]+=cnt[1][i^1];
for (i=2;i<=head;i+=2) if (!cnt[0][i]&&cnt[1][i]==tot) ans++;
printf("%d",ans);
}
signed main(void){
solve();
return 0;
}