codeforces 589H - Tourist Guide(dfs)

H - Tourist Guide
Time Limit:2000MS     Memory Limit:524288KB     64bit IO Format:%I64d & %I64u

Description

It is not that easy to create a tourist guide as one might expect. A good tourist guide should properly distribute flow of tourists across the country and maximize the revenue stream from the tourism. This is why there is a number of conditions to be met before a guide may be declared as an official tourist guide and approved by Ministry of Tourism.

Ministry of Tourism has created a list of k remarkable cities out of n cities in the country. Basically, it means that in order to conform to strict regulations and to be approved by the ministry a tourist guide should be represented as a set of routes between remarkable cities so that the following conditions are met:

  • the first and the last city of every route are distinct remarkable cities,
  • each remarkable city can be an endpoint of at most one route,
  • there is no pair of routes which share a road.

Please note that a route may pass through a remarkable city. Revenue stream from the tourism highly depends on a number of routes included in a tourist guide so the task is to find out a set of routes conforming the rules of a tourist guide with a maximum number of routes included.

Input

The first line contains three integer numbers n,  m,  k(1 ≤ n ≤ 50000,  0 ≤ m ≤ 50000,  1 ≤ k ≤ n) — the number of cities in the country, the number of roads in the country and the number of remarkable cities correspondingly.

Each of the following m lines contains two integer numbers ai and bi(1 ≤ ai, bi ≤ n) — meaning that cities ai and bi are connected by a bidirectional road. It is guaranteed that ai and bi are distinct numbers and there is no more than one road between a pair of cities.

The last line contains k distinct integer numbers — a list of remarkable cities. All cities are numbered from 1 to n.

Output

The first line of the output should contain c — the number of routes in a tourist guide. The following c lines should contain one tourist route each. Every route should be printed in a form of "tv1v2 ... vt + 1", where t is a number of roads in a route and v1,  v2, ..., vt + 1 — cities listed in the order they are visited on the route.

If there are multiple answers print any of them.

Sample Input

Input
6 4 4
1 2
2 3
4 5
5 6
1 3 4 6
Output
2
2 1 2 3
2 4 5 6
Input
4 3 4
1 2
1 3
1 4
1 2 3 4
Output
2
1 1 2
2 3 1 4



我发现我蠢得连建图都不会建。。。。
这道题虽然思路貌似并不难。。。但是写不出。。。因为我竟然不会用vector....
简直sad...
于是照猫画虎。。。熟悉一下vector...还有pair....
熟悉下建图。。。。。。
嗯。。慢慢来。
  1 /*************************************************************************
  2     > File Name: code/hust/20151025/H.cpp
  3     > Author: 111qqz
  4     > Email: rkz2013@126.com 
  5     > Created Time: 2015年10月27日 星期二 19时30分28秒
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<iomanip>
 10 #include<cstdio>
 11 #include<algorithm>
 12 #include<cmath>
 13 #include<cstring>
 14 #include<string>
 15 #include<map>
 16 #include<set>
 17 #include<queue>
 18 #include<vector>
 19 #include<stack>
 20 #include<cctype>
 21                  
 22 #define yn hez111qqz
 23 #define j1 cute111qqz
 24 #define ms(a,x) memset(a,x,sizeof(a))
 25 #define pb push_back
 26 using namespace std;
 27 const int dx4[4]={1,0,0,-1};
 28 const int dy4[4]={0,-1,1,0};
 29 typedef long long LL;
 30 typedef double DB;
 31 const int inf = 0x3f3f3f3f;
 32 const int N=5E4+7;
 33 
 34 int n,m,k;
 35 vector<int> adj[N];
 36 vector<pair< pair<int,int> ,int> > ans;
 37 int pa[N];
 38 int kt[N];
 39 bool spe[N];
 40 void init()
 41 {
 42     ms(spe,false);
 43     ms(pa,0);
 44     scanf("%d %d %d",&n,&m,&k);
 45     for ( int i = 0,u,v; i < m ; i++)
 46     {
 47     scanf("%d %d",&u,&v);
 48     adj[u].push_back(v);
 49     adj[v].push_back(u);
 50     }
 51 
 52     for ( int i = 0,u ; i < k ; i++)
 53     {
 54     scanf("%d",&u);
 55     spe[u] = true;
 56     }
 57 
 58 }
 59 
 60 void dfs( int u)
 61 {
 62  //   cout<<"u:"<<u<<endl;
 63     vector<int> tmp;
 64     tmp.clear();
 65 
 66     for ( int j = 0 ; j < adj[u].size(); j++)
 67     {
 68     int v =adj[u][j];
 69     if (pa[v]==0)
 70     {
 71         pa[v] =  u;
 72         dfs(v);
 73         if (kt[v]) tmp.push_back(kt[v]);
 74     }
 75     }
 76     while (tmp.size()>1)
 77     {
 78     int x1 = tmp.back();tmp.pop_back();
 79     int x2 = tmp.back(); tmp.pop_back();
 80 //    cout<<"x1:"<<x1<<" x2:"<<x2<<endl;
 81     ans.pb(make_pair(make_pair(x1,x2),u));
 82     }
 83     if (tmp.size()>0)
 84     {
 85     int x = tmp.back();tmp.pop_back();
 86 
 87 //    cout<<"x:"<<x<<endl;
 88     if (spe[u])
 89     {
 90         ans.push_back(make_pair(make_pair(x,u),u));
 91     }
 92     else kt[u] = x;
 93     }
 94     else 
 95     {
 96     if (spe[u]) kt[u] =  u;
 97     }
 98 }
 99 
100 void solve()
101 {
102     for ( int i = 1 ; i <n+1 ; i++)
103     if (!pa[i])
104     {
105         pa[i] = -1;
106         dfs(i);
107     }
108 
109     vector<int>p,q;
110    // printf("%d\n",ans.size());
111       cout<<ans.size()<<endl;
112     for ( int i = 0 ; i < ans.size(); i++)
113     {
114     int u = ans[i].first.first;
115     int v = ans[i].first.second;
116     int r = ans[i].second;
117 //    cout<<"************************"<<endl;
118 //    cout<<"u:"<<u<<" v:"<<v<<" r:"<<r<<endl;
119 //    cout<<"************************"<<endl<<endl;
120 
121     p.clear();
122     q.clear();
123     while (u!=r)
124     {
125         p.push_back(u);
126         u = pa[u];
127     }
128 
129     while (v!=r)
130     {
131         q.push_back(v);
132         v = pa[v];
133     }
134 
135     //printf("%d ",p.size()+q.size());
136     cout<<p.size()+q.size()<<" ";
137     for ( int j = 0 ; j < p.size(); j++) printf("%d ",p[j]);
138     printf("%d ",r);
139     
140     reverse(q.begin(),q.end());
141     for ( int j = 0 ; j < q.size(); j++) printf("%d ",q[j]);
142     puts("");    
143     
144     }
145 }
146 int main()
147 {
148   #ifndef  ONLINE_JUDGE 
149    freopen("in.txt","r",stdin);
150   #endif
151    init();
152    solve();
153   
154    
155  #ifndef ONLINE_JUDGE  
156   fclose(stdin);
157   #endif
158     return 0;
159 }
View Code

 

posted @ 2015-10-27 21:04  111qqz  阅读(285)  评论(0编辑  收藏  举报