[bzoj1301] [LLH邀请赛]参观路线

  本题同bzoj1098

  用个并查集,把连续的被访问过的点并起来。。这样就不会尝试已经走过的点了。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=100233,maxm=1002333;
 7 struct zs{
 8     int too,pre;
 9 }e[maxm<<1],e1[maxm<<1];int tot,last[maxn],tot1,last1[maxn];
10 int u[maxm],v[maxm];
11 int fa[maxn];
12 int i,j,k,n,m;
13  
14 inline void insert(int a,int b){
15     if(e[last[a]].too==b)return;
16     e[++tot].too=b,e[tot].pre=last[a],last[a]=tot;
17 }
18 inline void ins(int a,int b){
19     e1[++tot1].too=b,e1[tot1].pre=last1[a],last1[a]=tot1;
20 }
21 int getfa(int x){
22     return fa[x]!=x?fa[x]=getfa(fa[x]):x;
23 }
24  
25 void dfs(int x){
26     fa[x]=x+1;
27     printf("%d\n",x);
28     int next=getfa(1);//printf("  next:%d\n",next);
29      
30     for(int i=last[x];i;i=e[i].pre){
31         while(next<e[i].too)
32             dfs(next),next=getfa(next+1);
33         if(next==e[i].too)next=getfa(next+1);
34     }
35     while(next<=n)dfs(next),next=getfa(next+1);
36 }
37  
38 int ra;char rx;
39 inline int read(){
40     rx=getchar(),ra=0;
41     while(rx<'0'||rx>'9')rx=getchar();
42     while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra;
43 }
44 int main(){
45     n=read(),m=read();
46     for(i=1;i<=m;i++)u[i]=read(),v[i]=read(),ins(v[i],i),ins(u[i],i);
47     for(i=n;i;fa[i]=i,i--)
48         for(j=last1[i];j;j=e1[j].pre)
49             if(j&1)insert(u[e1[j].too],i);
50             else insert(v[e1[j].too],i);
51 //  for(i=1;i<=n;i++)for(j=last[i];j;j=e[j].pre)printf("%d-->%d\n",i,e[j].too);//return 233;
52     fa[n+1]=n+1;
53     dfs(1);
54     return 0;
55 }
56 
View Code

 

posted @ 2016-06-19 13:03  czllgzmzl  阅读(188)  评论(0编辑  收藏  举报