E 定向 牛客练习赛25
tarjan
父节点和子节点
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <time.h> 6 #include <string> 7 #include <set> 8 #include <map> 9 #include <list> 10 #include <stack> 11 #include <queue> 12 #include <vector> 13 #include <bitset> 14 #include <ext/rope> 15 #include <algorithm> 16 #include <iostream> 17 using namespace std; 18 #define ll long long 19 #define minv 1e-6 20 #define inf 1e9 21 #define pi 3.1415926536 22 #define E 2.7182818284 23 const ll mod=1e9+7;//998244353 24 const int maxn=1e6+10; 25 26 struct node 27 { 28 int d,pos,c; 29 node* next; 30 }*e[maxn]; 31 32 bool vis[maxn]={0}; 33 int dfn[maxn],low[maxn],num=0,g=0,n,cond[maxn],st[maxn],fa[maxn]; 34 35 void dfs(int d) 36 { 37 int dd; 38 node* p=e[d]; 39 dfn[d]=low[d]=++num; 40 vis[d]=1; 41 st[++g]=d; 42 while (p) 43 { 44 dd=p->d; 45 if (!vis[dd]) 46 { 47 fa[dd]=d; 48 dfs(dd); 49 low[d]=min(low[d],low[dd]); 50 cond[p->pos]=p->c; 51 } 52 //lack whether in stack (not necessarily) 53 else if (fa[d]!=dd) 54 { 55 low[d]=min(low[d],dfn[dd]); 56 if (dfn[d]>dfn[dd]) 57 cond[p->pos]=p->c; 58 } 59 p=p->next; 60 } 61 if (dfn[d]==low[d]) 62 { 63 if (g!=n || st[1]!=d) 64 { 65 printf("impossible"); 66 exit(0); 67 } 68 } 69 } 70 71 int main() 72 { 73 node* p; 74 int m,i,x,y; 75 scanf("%d%d",&n,&m); 76 for (i=1;i<=m;i++) 77 { 78 scanf("%d%d",&x,&y); 79 p=(node*) malloc (sizeof(node)); 80 p->d=y; 81 p->pos=i; 82 p->c=0; 83 p->next=e[x]; 84 e[x]=p; 85 86 p=(node*) malloc (sizeof(node)); 87 p->d=x; 88 p->pos=i; 89 p->c=1; 90 p->next=e[y]; 91 e[y]=p; 92 } 93 fa[1]=0; 94 dfs(1); 95 for (i=1;i<=m;i++) 96 printf("%d",cond[i]); 97 return 0; 98 } 99 /* 100 3 2 101 1 2 102 2 3 103 */