Esempio n. 1
0
int main()
{
  int t;
  int ng,nm;
  int i,temp;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d %d",&ng,&nm);
    g.clear(); m.clear();
    for(i=0;i<ng;++i) 
    {
      scanf("%d",&temp);
      g.insert(temp);
    }
    for(i=0;i<nm;++i) 
    {
      scanf("%d",&temp);
      m.insert(temp);
    }

    if(solve()) printf("Godzilla\n");
    else printf("MechaGodzilla\n");
  }
  return 0;
}
void dfs2(int node, int depth)
{
    visited[node] = true;
    
    if(depth == farLen)
        worst.insert(node);
    
    if(depth > farLen)
    {
        farLen = depth;
        worst.clear();
        worst.insert(node);
    }
    
    for(si_it a = adjList[node].begin();
              a != adjList[node].end(); ++a)
    {
        if(!visited[*a])
            dfs2(*a, depth + 1);
    }
}
Esempio n. 3
0
int main()
{
    int n,m,x,y,i;
    while(scanf("%d%d", &n, &m)!=EOF)
    {
        vs.clear();
        for(i=0; i<n; ++i) v[i].clear();

        for(i=0; i<m; ++i)
        {
            scanf("%d%d", &x, &y);
            --x; --y;
            v[x].push_back(y);
            v[y].push_back(x);
        }

        for(i=0; i<n; ++i)
        {
            s1.insert(i);
            sort(v[i].begin(), v[i].end());
        }

        while(!s1.empty())
        {
            x=*s1.begin();
            s1.erase(x);

            s2.clear();

            q.push(x);

            while(!q.empty())
            {
                x=q.front(); q.pop();
                s2.insert(x);

                for(sit=s1.begin(); sit != s1.end();)
                {
                    y=*sit;
                    ++sit;
                    if(!binary_search(v[x].begin(), v[x].end(), y))
                    {
                        q.push(y); s1.erase(y);
                    }
                }
            }
            vs.push_back(s2);
        }
        printf("%u\n", vs.size());
        for(i=0; i<vs.size(); ++i)
        {
            printf("%u", vs[i].size());
            for(sit=vs[i].begin(); sit != vs[i].end(); ++sit)
            {
                printf(" %d", *sit+1);
            }
            puts("");
        }
    }
    return 0;
}
int main()
{
    int cases;
    cin >> cases;
    
    int tn;
    for(int tcase = 1; tcase <= cases; ++tcase)
    {
        s1.clear(); 
        s2.clear();
        adjList.clear();
        adjMat.clear();
        parent.clear();
        s1Size = 0;
        s2Size = 0;
        start = 0;
        sink = 0;
        
        
        // Get input, Build adjacency list + matrix
        cin >> s1Size;
        for(int n = 0; n < s1Size; ++n)
        {
            scanf("%i", &tn);
            s1.insert(tn);
            adjList[tn] = si();
            adjMat[tn] = mii();
        }
        
        cin >> s2Size;
        for(int n = 0; n < s2Size; ++n)
        {
            scanf("%i", &tn);
            s2.insert(tn);
            adjList[tn] = si();
            adjMat[tn] = mii();
            
            for(si_it a = s1.begin(); a != s1.end(); ++a)
                if((tn == 0 && *a == 0) || tn == 0 || 
                   (*a != 0 && tn % (*a) == 0))
                {
                    adjList[*a].insert(tn);
                    adjMat[*a][tn] = 1;
                    adjMat[tn][*a] = 1;
                }
                else
                {
                    adjMat[*a][tn] = 0;
                    adjMat[tn][*a] = 0;
                }
        }
        
        for(start = 0; s1.count(start) || s2.count(start); ++start) {}
        for(sink = 1; s1.count(sink) || s2.count(sink) || 
                      sink == start; ++sink) {}
        
        adjList[start] = si();
        adjList[sink] = si();
        adjMat[start] = mii();
        adjMat[sink] = mii();
        
        for(si_it a = s1.begin(); a != s1.end(); ++a)
        {
            adjList[start].insert(*a);
            adjMat[start][*a] = 1;
            adjMat[*a][start] = 1;
        }
        for(si_it a = s2.begin(); a != s2.end(); ++a)
        {
            adjList[*a].insert(sink);
            adjMat[*a][sink] = 1;
            adjMat[sink][*a] = 1;
        }
        
        
        // Edmonds Karp's
        int maxFlow = 0;
        
        while(true)
        {
            queue<int> bfsQ;
            set<int> visited;
            tFlow = 0;
            
            bfsQ.push(start);
            visited.insert(start);
            
            while(!bfsQ.empty())
            {
                int tn = bfsQ.front();
                bfsQ.pop();
                
                if(tn == sink)
                    break;
                    
                for(si_it a = adjList[tn].begin();
                          a != adjList[tn].end(); ++a)
                {
                    if(adjMat[tn][*a] > 0 && !visited.count(*a))
                    {
                        visited.insert(*a);
                        parent[*a] = tn;
                        bfsQ.push(*a);
                    }
                }
            }
            
            augmentPath(sink, INF);
            if(tFlow == 0)
                break;
            maxFlow += tFlow;
        }
        
        printf("Case %i: %i\n", tcase, maxFlow);
    }
    
    return 0;
}
int main()
{
    while(cin >> nNodes)
    {
        worst.clear();
        best.clear();
        for(int i = 1; i <= nNodes; ++i)
            adjList[i].clear();
            
        int nAdj;
        for(int n = 1; n <= nNodes; ++n)
        {
            scanf("%i", &nAdj);
            int tempn;
            for(int a = 0; a < nAdj; ++a)
            {
                scanf("%i", &tempn);
                adjList[n].insert(tempn);
                adjList[tempn].insert(n);
            }
        }
        
        farLen = farNode = 0;
        memset(visited, 0, sizeof(visited));
        dfs1(1, 0);
        
        farLen = 0;
        memset(visited, 0, sizeof(visited));
        dfs2(farNode, 0);
        
        memset(visited, 0, sizeof(visited));
        dfs2(*(worst.begin()), 0);
        
        memset(visited, 0, sizeof(visited));
        for(int i = 1; i <= nNodes; ++i) height[i] = INF;
        dfs3(*(worst.begin()), 0);
        
        int minH = INF+1;
        for(int i = 1; i <= nNodes; ++i)
        {
            if(height[i] == minH)
                best.insert(i);
                
            if(height[i] < minH)
            {
                best.clear();
                best.insert(i);
                minH = height[i];
            }
        }
        
        
        if(nNodes == 1)
        {
            best.clear();
            worst.clear();
            
            best.insert(1);
            worst.insert(1);
        }
        
        printf("Best Roots  :");
        
        for(si_it it = best.begin();
                    it != best.end(); ++it)
            printf(" %i", *it);
        cout << endl;
        
        printf("Worst Roots :");
        
        for(si_it it = worst.begin();
                    it != worst.end(); ++it)
            printf(" %i", *it);
        cout << endl;
    }
    
    return 0;
}