BZOJ4424: Cf19E Fairy

树上差分的代码很简洁,dfs+差分即可

这题很多坑点啊,比如重边自环好坑

 #include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<vector>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define ft first
#define sc second
#define MAXN 1000000+10
using namespace std;
int first[MAXN],edges[MAXN<<],nxt[MAXN<<],to[MAXN<<];
int n,m,c=-;
int b[MAXN],d[MAXN],tag[MAXN];
int tmp,cnt,self_tmp;
void insert(int x,int y,int e){
nxt[++c]=first[x],first[x]=c,to[c]=y,edges[c]=e;
nxt[++c]=first[y],first[y]=c,to[c]=x,edges[c]=e;
}
void init(){
scanf("%d%d",&n,&m);
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
if(x==y&&!self_tmp){self_tmp=i;continue;}
if(x==y){self_tmp=-;continue;}
insert(x,y,i);
}
}
void dfs(int x,int fa){
for(int e=first[x];e;e=nxt[e]){
if(e==(fa^))continue;
int &y=to[e];
if(d[y]){
if(d[x]<d[y])continue;
if((d[x]-d[y]+)&){
tag[x]++,tag[y]--;
cnt++;tmp=edges[e];
}
else{
tag[x]--,tag[y]++;
}
}
else{
d[y]=d[x]+;
dfs(y,e);
tag[x]+=tag[y];
}
}
}
vector<int> ans;
void find(int x){
b[x]=;
for(int e=first[x];e;e=nxt[e]){
int &y=to[e];
if(b[y])continue;
if(tag[y]==cnt){
ans.pb(edges[e]);
}
find(y);
}
}
void solve(){
if(-==self_tmp){printf("0\n");return;}
for(int i=;i<=n;i++){
if(!d[i])d[i]=,dfs(i,);
}
if(!cnt){
if(self_tmp){printf("1\n%d\n",self_tmp);}
else{printf("%d\n",m);for(int i=;i<m;i++)printf("%d ",i);printf("%d\n",m);}
return;
}
if(self_tmp){printf("0\n");return;}
for(int i=;i<=n;i++){
if(!b[i])find(i);
}
if(cnt==)ans.pb(tmp);
sort(ans.begin(),ans.end());
printf("%d\n",ans.size());
for(int i=;i<ans.size();i++){
printf("%d",ans[i]);
if(i==ans.size()-)printf("\n");
else printf(" ");
}
}
int main()
{
// freopen("data.in","r",stdin);
// freopen("my.out","w",stdout);
init();
solve();
return ;
}
上一篇:SQL Server 2008R2 企业版 百度云下载地址


下一篇:!DOCTYPE 文档类型声明