Graph Without Long Directed Paths
生活随笔
收集整理的這篇文章主要介紹了
Graph Without Long Directed Paths
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://codeforces.com/contest/1144/problem/F
題意:給定一個無向圖,要求添加方向使得圖變為有向圖并且不存在長度大于1的路徑
C++版本一
題解:二分匹配
對于一個點的所有有關系的邊中只能有指向它的或者背向它的;
所有二分匹配,標記這個點是什么情況。然后枚舉邊的出發點,輸出出發點的情況就行了
/* *@Author: STZG *@Language: C++ */ #include <bits/stdc++.h> #include<iostream> #include<algorithm> #include<cstdlib> #include<cstring> #include<cstdio> #include<string> #include<vector> #include<bitset> #include<queue> #include<deque> #include<stack> #include<cmath> #include<list> #include<map> #include<set> //#define DEBUG #define RI register int #define endl "\n" using namespace std; typedef long long ll; //typedef __int128 lll; const int N=200000+10; const int M=100000+10; const int MOD=1e9+7; const double PI = acos(-1.0); const double EXP = 1E-8; const int INF = 0x3f3f3f3f; int t,n,m,k,p,l,r,u,v; int ans,cnt,flag,temp,sum; int color[N]; int a[N]; char str; vector<int>G[N]; void dfs(int u,int c){color[u]=c;for(int i=0,j=G[u].size();i<j;i++){int v=G[u][i];if(color[u]==color[v]){flag=0;}if(color[v]==-1)dfs(v,!c);} } int main() { #ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout); #endif//ios::sync_with_stdio(false);//cin.tie(0);//cout.tie(0);//scanf("%d",&t);//while(t--){scanf("%d%d",&n,&m);for(int i=1;i<=m;i++){scanf("%d%d",&u,&v);G[u].push_back(v);G[v].push_back(u);a[i]=u;}memset(color,-1,sizeof(color));flag=1;for(int i=1;i<=n;i++){if(color[i]==-1)dfs(i,0);}if(flag){cout<<"YES"<<endl;for(int i=1;i<=m;i++){cout<<!color[a[i]];}}else{cout<<"No"<<endl;}//}#ifdef DEBUGprintf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC); #endif//cout << "Hello world!" << endl;return 0; }C++版本二
#include<bits/stdc++.h> #define fi first #define sf scanf #define se second #define pf printf #define pb push_back #define mp make_pair #define sz(x) ((int)(x).size()) #define all(x) (x).begin(),(x).end() #define mem(x,y) memset((x),(y),sizeof(x)) #define fup(i,x,y) for(int i=(x);i<=(y);++i) #define fdn(i,x,y) for(int i=(x);i>=(y);--i) typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef std::pair<int,int> pii; using namespace std;const int __=2e5+5;struct node {int x,c,id; };vector<node>G[__]; int col[__],ans[__];void dfs(int x,int fa=-1,int c=0) {if(col[x]){if(col[x]!=c){puts("NO");exit(0);}return;}col[x]=c;for(int i=0;i<sz(G[x]);++i){node y=G[x][i];if(y.x!=fa){ans[y.id]=y.c^c;dfs(y.x,x,1-c);}} }int main() {int n,m;sf("%d%d",&n,&m);fup(i,1,m){int x,y;sf("%d%d",&x,&y);G[x].pb({y,1,i});G[y].pb({x,0,i});}dfs(1);puts("YES");fup(i,1,m)pf("%d",ans[i]);return 0; }?
總結
以上是生活随笔為你收集整理的Graph Without Long Directed Paths的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Median String
- 下一篇: Two Merged Sequences