ZOJ3378 两点间的必经之路 tarjan算法

Attack the NEET Princess

Time Limit: 9 Seconds      Memory Limit: 65536 KB

Houraisan Kaguya ( 蓬萊山輝夜) is the NEET (not in education, employment or training) princess living in Eientei ( 永遠亭). She is an unemployed geek, who sits at home and surfs the Internet all day. But today is different, as the Hakurei Shrine Reitaisai Festival ( 博麗神社例大祭) will be hosted in Hakurei Shrine , Kaguya decides to go to buy some new doujin games.

ZOJ3378 两点间的必经之路 tarjan算法_第1张图片

Fujiwara no Mokou ( 藤原妹紅) detests Kaguya, and has been planing to attack her for a long time. But she nerver got the chance before because Kaguya kept staying in Eientei , under the protection of Yagokoro Eirin ( 八意永琳). Knowing that Kaguya will go outside today, Mokou decides to wait on certain road and attack her once she passes there. Mokou don't know which path Kaguya will choose, but she thinks that there always exits some roads where she can always meet Kaguya.

Input

There are multiple cases. Each case begins with two integers 2 ≤ n ≤ 10000 -- the number of villages and 2 ≤ m ≤ 100000 -- the number of roads. Then m lines, each contains two integers a and b (0 ≤ a , b < n), indicating a road connecting village a and village b . Village 0 is Eientei and Village n -1 is Hakurei Shrine . They are always connected by roads. There may be more than one roads between two villages.

Output

Find out the roads where Mokou can always meet Kaguya. For each case, output the number of roads in the first line, then output the numbers of the roads in ascending order in the second line.

Sample Input

3 2

0 1

1 2

 

7 8

0 1

0 2

1 3

2 3

3 4

3 5

4 6

5 6

 

Sample Output

2

0 1

0

 

 

求起点到终点之间的必经之路 , 思路是 : 首先用 tarjan 算法求出图中的所有割边 , 然后从起点开始 BFS 到终点搜出一条路径 , 把路径上的边和已求出的割边求交集就可以得出必经之路 .

 

代码如下 :

 #include<stdio.h> #include<string.h> #include<stack> #define MAXN 10005 #include<algorithm> using std::sort; using namespace std; struct E { int nod,db,cut,num; E *next; }head[MAXN*10]; int low[MAXN],dfn[MAXN],res[10*MAXN],cnt; bool mark[MAXN],vis[MAXN]; stack <int> ss; void tarjan(int u) { int v; E *p,*s,*q; low[u]=dfn[u]=cnt++; ss.push(u); vis[u]=1; mark[u]=1; for (p=head[u].next;p!=NULL;p=p->next) { v=p->nod; if (!mark[v]) { if (!p->db) { s=head[v].next; q=&head[v]; while (s->nod!=u) {q=s;s=s->next;} q->next=s->next; delete(s); } mark[v]=1; vis[v]=1; ss.push(v); tarjan(v); low[u]=min(low[u],low[v]); } else if (vis[v]) low[u]=min(low[u],dfn[v]); } if (low[u]==dfn[u]) { while (!ss.empty()&&ss.top()!=u) ss.pop(); ss.pop(); } } bool DFS(int b,int e) { E *p; int v; if (b==e) return 1; p=head[b].next; while (p!=NULL) { v=p->nod; if (!mark[v]) { if (DFS(v,e)==1) { if (p->cut==1) p->cut=2; return 1; } } } return 0; } bool find(int a,int b) { E *p; p=head[a].next; while (p!=NULL) { if (p->nod==b) return 1; p=p->next; } return 0; } int main() { int n,m,i,a,b,total; E *s,*p; while (scanf("%d%d",&n,&m)!=EOF) { memset(mark,0,sizeof(mark)); memset(vis,0,sizeof(vis)); memset(head,0,sizeof(head)); for (i=1;i<=m;++i) { scanf("%d%d",&a,&b); a++;b++; if (!find(a,b)) { s=new E; s->nod=b; s->db=0; s->cut=0; s->num=i; s->next=head[a].next; head[a].next=s; s=new E; s->nod=a; s->db=0; s->cut=0; s->num=i; s->next=head[b].next; head[b].next=s; } else { s=head[a].next; while (s!=NULL&&s->nod!=b) s=s->next; s->db=1; s=head[b].next; while (s!=NULL&&s->nod!=a) s=s->next; s->db=1; } } cnt=1; tarjan(1); for (i=1;i<=n;++i) { p=head[i].next; while (p!=NULL) { if (dfn[i]<low[p->nod]) p->cut=1; p=p->next; } } memset(mark,0,sizeof(mark)); DFS(1,n); total=0; for (i=1;i<=n;++i) { p=head[i].next; while (p!=NULL) { if (p->cut==2) res[++total]=p->num; p=p->next; } } sort(res+1,res+total+1); printf("%d/n",total); for (i=1;i<=total;++i) { printf("%d",res[i]-1); if (i!=total) printf(" "); else printf("/n"); } for (i=1;i<=n;++i) { p=head[i].next; s=p; while (s!=NULL) { s=s->next; delete(p); p=s; } } } return 0; } 

 

你可能感兴趣的:(算法,null,delete,each,output,Training)