[模板]黑白染色
如題:模板保存。
#include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<iostream> #include<cmath> using namespace std; typedef long long ll; const int maxn=1e5+5; const int maxm=1e5+5;int c[maxn]; //color,每個點的黑白屬性,-1表示還沒有標記,0/1表示黑白,比vis數組多一個作用 int num[2]; //在一次DFS中的黑白點個數 bool f=0; //判斷是否出現奇環int head[maxn],tot,n,m; struct edge{int to;int nxt; }e[maxm]; void add_edge(int u,int v){e[tot].to=v;e[tot].nxt=head[u];head[u]=tot++; } void init(){memset(head,-1,sizeof head);tot=0;memset(c,-1,sizeof c); }void dfs(int u,int x){if(f)return;c[u]=x;num[x]++;for(int i=head[u];~i;i=e[i].nxt){int j=e[i].to;if(c[j]==-1) dfs(j,!x);else if(c[j]==x){//沖突了,不沖突則跳過。f=1;return;}} } int main(){for(int i=1;i<=n&&(!f);i++){if(c[i]==-1){num[0]=num[1]=0;dfs(i,1);}} }?
轉載于:https://www.cnblogs.com/elpsycongroo/p/10356610.html
總結
- 上一篇: 437. Path Sum III
- 下一篇: k8s组件批量启动、查看状态