标签:
一开始以为直接算联通块个数就行了
后来发现还得分联通块里的奇点。。。
还要注意m = 0的情况...
1 #include<iostream> 2 #include<algorithm> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<cstring> 6 #include<string> 7 8 using namespace std; 9 10 void setIO(const string& a) { 11 freopen((a+".in").c_str(), "r", stdin); 12 freopen((a+".out").c_str(), "w", stdout); 13 } 14 15 const int N = 1000 + 10; 16 17 #include<vector> 18 vector<int> G[N]; 19 bool vis[N]; 20 int du[N]; 21 22 int dfs(int u) { 23 vis[u] = 1; 24 int res = G[u].size() & 1; 25 for(unsigned i = 0; i < G[u].size(); i++) { 26 int v = G[u][i]; 27 if(!vis[v]) res += dfs(v); 28 } 29 return res; 30 } 31 32 33 int main() { 34 #ifdef DEBUG 35 freopen("in.txt", "r", stdin); 36 freopen("out.txt", "w", stdout); 37 #endif 38 39 int n, m, T, cas = 0; 40 while(scanf("%d%d%d", &n, &m, &T) && n) { 41 for(int i = 1; i <= n; i++) { 42 G[i].clear(); 43 du[i] = 0; 44 vis[i] = 0; 45 } 46 for(int i = 1; i <= m; i++) { 47 int u, v; 48 scanf("%d%d", &u, &v); 49 G[u].push_back(v); 50 G[v].push_back(u); 51 du[u]++, du[v]++; 52 } 53 int c1 = 0, c2 = 0; 54 for(int i = 1; i <= n; i++) if(!vis[i] && du[i]) { 55 c1++; c2 += max((dfs(i) - 2) / 2, 0); 56 } 57 58 printf("Case %d: %d\n", ++cas, T * (max(c1 - 1, 0) + c2 + m)); 59 } 60 61 return 0; 62 }
标签:
原文地址:http://www.cnblogs.com/showson/p/4978839.html