MZL‘s City
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
MZL is an active girl who has her own country. Her big country has N cities numbered from 1 to N.She has controled the country for so long and she only remebered that there was a big earthquake M years ago,which made all the roads between the cities destroyed and all the city became broken.She also remebered that exactly one of the following things happened every recent M years: 1.She rebuild some cities that are connected with X directly and indirectly.Notice that if a city was rebuilt that it will never be broken again. 2.There is a bidirectional road between city X and city Y built. 3.There is a earthquake happened and some roads were destroyed. She forgot the exactly cities that were rebuilt,but she only knew that no more than K cities were rebuilt in one year.Now she only want to know the maximal number of cities that could be rebuilt.At the same time she want you to tell her the smallest lexicographically plan under the best answer.Notice that 8 2 1 is smaller than 10 0 1.
Input
The first contains one integer T(T<=50),indicating the number of tests. For each test,the first line contains three integers N,M,K(N<=200,M<=500,K<=200),indicating the number of MZL’s country ,the years happened a big earthquake and the limit of the rebuild.Next M lines,each line contains a operation,and the format is “1 x” , “2 x y”,or a operation of type 3. If it’s type 3,first it is a interger p,indicating the number of the destoyed roads,next 2*p numbers,describing the p destoyed roads as (x,y).It’s guaranteed in any time there is no more than 1 road between every two cities and the road destoyed must exist in that time.
Output
The First line Ans is the maximal number of the city rebuilt,the second line is a array of length of tot describing the plan you give(tot is the number of the operation of type 1).
Sample Input
1 5 6 2 2 1 2 2 1 3 1 1 1 2 3 1 1 2 1 2
Sample Output
3 0 2 1 Hint No city was rebuilt in the third year,city 1 and city 3 were rebuilt in the fourth year,and city 2 was rebuilt in the sixth year.
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cmath> #include<stdlib.h> #include<map> #include<set> #include<time.h> #include<vector> #include<queue> #include<string> #include<string.h> #include<iostream> #include<algorithm> using namespace std; #define eps 1e-8 #define INF 0x3f3f3f3f #define LL long long #define max(a,b) ((a)>(b)?(a):(b)) #define min(a,b) ((a)<(b)?(a):(b)) typedef pair<int , int> P; #define max_n 200 + 10 #define max_m 500 + 10 struct Node1 { int time, x; }; struct Node2 { int time, x, y; }; struct Node3 { int time, p; vector<P> V; }; Node1 s1[max_m]; Node2 s2[max_m]; Node3 s3[max_m]; int g[max_n][max_n]; int n, m, k; int vis[max_n]; int mark[max_n]; int num[max_n]; int cnt1, cnt2, cnt3; vector<int> ans; int cnt; int sum; void dfs(int u) { if(cnt == k) return ; //cout<<u<<" "; vis[u] = 1; if(mark[u]) { num[u] = 0; } else { cnt++; num[u] = 1; mark[u] = 1; } for(int v = 1; v <= n; v++) if(!vis[v] && g[u][v]) { dfs(v); num[u] += num[v]; } return ; } void solve() { for(int t = m; t > 0; t--) { if(cnt1 == 0) break ; if(s1[cnt1].time == t) { memset(vis, 0, sizeof vis); int x = s1[cnt1].x; cnt = 0; //cout<<"#"; dfs(x); //cout<<endl; sum += cnt; ans.push_back(cnt); cnt1--; } else if(s2[cnt2].time == t) { int x = s2[cnt2].x, y = s2[cnt2].y; g[x][y] = g[y][x] = 0; cnt2--; } else { int x, y; for(int i = 0; i < s3[cnt3].p; i++) { x = s3[cnt3].V[i].first, y = s3[cnt3].V[i].second; g[x][y] = g[y][x] = 1; } cnt3--; } } printf("%d\n", sum); for(int i = ans.size() - 1; i >= 0; i--) i == 0 ? printf("%d\n", ans[i]) : printf("%d ", ans[i]); } int main() { int T; scanf("%d", &T); while(T--) { sum = 0; ans.clear(); memset(mark, 0, sizeof mark); cnt1 = cnt2 = cnt3 = 0; scanf("%d%d%d", &n, &m, &k); int op, u, v; for(int i = 1; i <= m; i++) { scanf("%d%d", &op, &u); if(op == 1) { s1[++cnt1] = Node1{i, u}; } else if(op == 2) { scanf("%d", &v); s2[++cnt2] = Node2{i, u, v}; g[u][v] = g[v][u] = 1; } else { s3[++cnt3].p = u; s3[cnt3].time = i; int x, y; s3[cnt3].V.clear(); for(int j = 0; j < u; j++) { scanf("%d%d", &x, &y); s3[cnt3].V.push_back({x, y}); g[x][y] = g[y][x] = 0; } } } solve(); } return 0; } /* 100 5 7 5 2 1 2 2 2 3 2 3 4 2 4 5 1 2 3 1 2 3 1 3 5 7 5 2 1 2 2 2 3 2 3 4 2 4 5 1 2 3 1 2 3 1 2 4 7 3 2 1 2 2 2 3 2 3 4 2 4 1 1 4 3 1 1 2 1 1 5 7 3 2 1 2 2 2 3 2 3 5 2 3 4 1 1 3 1 2 3 1 2 */
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-13 05:46:22