Kaidul

11307

Oct 29th, 2013
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.70 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <vector>
  6. #include <stack>
  7. #include <queue>
  8. #include <map>
  9. #include <algorithm>
  10. #include <climits>
  11. using namespace std;
  12.  
  13. /**
  14. state:
  15.     dp[i][j] - minimum color sum at node i colored color j
  16. transition:
  17.     dp[i][j] += min(dp[k][l]), for all k is i's child and for all l of 15 colors
  18. so the dfs method would be like
  19. and the target state is min(dp[k][i]), for any arbitrary node k as root (I assumed 0 here), and for all i of 8 colors
  20. **/
  21.  
  22. #define Max 10005
  23. #define required_Color 8
  24. vector <int> adj[Max];
  25. int N, par[Max], in[Max], dp[Max][20];
  26. char input[Max * 2];
  27.  
  28. void dfs(int node) {
  29.     for(int i = 0; i < (int) adj[node].size(); i++) {
  30.         if(adj[node][i] != par[node]) {
  31.             par[adj[node][i]] = node;
  32.             dfs(adj[node][i]);
  33.         }
  34.     }
  35.     for(int i = 1; i < required_Color; i++) {
  36.         int ret = i;
  37.         for(int j = 0; j < (int) adj[node].size(); j++) {
  38.             if(adj[node][j] != par[node]) {
  39.                 int ans = INT_MAX;
  40.                 for(int k = 1; k < required_Color; k++)
  41.                     if(dp[ adj[node][j] ][k] != INT_MAX && k != i)
  42.                         ans = min(ans, dp[adj[node][j]][k] );
  43.                 if(ans != INT_MAX)
  44.                     ret += ans;
  45.             }
  46.         }
  47.         dp[node][i] = ret;
  48.     }
  49. }
  50.  
  51. int main() {
  52.     freopen("input.txt","r",stdin);
  53.     while(~scanf(" %d",&N) && N) {
  54.         // reset data-structure
  55.         for(int i = 0; i < N; i++) {
  56.             adj[i].clear();
  57.             in[i] = 0;
  58.             par[i] = -1;
  59.             for(int j = 0; j < 20; j++)
  60.                 dp[i][j] = INT_MAX;
  61.         }
  62.         // input
  63.         gets(input);
  64.         int j;
  65.         for(int i = 0; i < N; i++) {
  66.             gets(input);
  67.             char *ptr = strtok(input, " :");
  68.             sscanf(ptr, " %d", &i);
  69.             while( (ptr = strtok(NULL, " :")) != NULL) {
  70.                 sscanf(ptr, " %d", &j);
  71.                 in[j]++;
  72.                 adj[i].push_back(j);
  73.             }
  74.         }
  75.         // persuing root's index
  76.         int root;
  77.         for(int i = 0; i < N; i++) {
  78.             // for considering 0 as root
  79.             if(in[i] == 0) {
  80.                 root = i;
  81.                 break;
  82.             }
  83.         }
  84.         par[root] = root;
  85.         dfs(root);
  86.         if(N == 1)
  87.             printf("1\n");
  88.         else {
  89.             int ans = INT_MAX;
  90.             // trying with different number of color to get the optimal result
  91.             for(int j = 1; j < required_Color; j++)
  92.                 ans = min(ans, dp[root][j]);
  93.             printf("%d\n", ans);
  94.         }
  95.     }
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment