码迷,mamicode.com
首页 > 其他好文 > 详细

google code jam -- bad horse

时间:2014-10-08 01:24:14      阅读:298      评论:0      收藏:0      [点我收藏+]

标签:des   color   io   os   ar   for   sp   div   art   

二分图解决这个问题的思路
 
#include <stdio.h>
#include <string>
#include <iostream>
#include <vector>
#include <unordered_map>
#include <queue>
using namespace std;
 
class Node{
public:
    string name;
    vector<Node*> adj;
    bool visited;
    int groupId;
 
    Node(string str):name(str), visited(false), groupId(-1){}
    int getNeighborCount() {
        return adj.size();
    }
};
 
class Graph{
public:
    unordered_map<string, Node*> hashNode;
    vector<Node*> nodes;
 
    void createNode(string str)
    {
        Node* n = new Node(str);
        nodes.push_back(n);
        hashNode[str] = n;
    }
 
    bool isExist(string str)
    {
        if(hashNode.count(str)>0) return true;
        else return false;
    }
 
    void addEdge(string str1, string str2)
    {
        if(!isExist(str1)) createNode(str1);
        if(!isExist(str2)) createNode(str2);
 
        hashNode[str1]->adj.push_back( hashNode[str2] );
        hashNode[str2]->adj.push_back( hashNode[str1] );
    }
 
    bool isBipartite()
    {
        for(int i=0; i<nodes.size(); i++)
        {
            if(!nodes[i]->visited)
            {
                bool f = bfs(nodes[i]);
                if(!f) return false;
            }
        }
 
        return true;
    }
     
    bool bfs(Node* n)
    {
        n->groupId = 1;
 
        queue<Node*> q;
        q.push(n);
 
        while(!q.empty())
        {
            n = q.front(); q.pop();
            n->visited = true;
 
            for(int i=0; i<n->getNeighborCount(); i++)
            {
                if(!n->adj[i]->visited)
                {
                    n->adj[i]->groupId = 3 - n->groupId;
                    q.push(n->adj[i]);
                }
                else
                {
                    if(n->adj[i]->groupId == n->groupId)
                        return false;
                }
            }
        }
 
        return true;
    }
 
    ~Graph()
    {
        for(int i=0; i<nodes.size(); i++)
        {
            delete nodes[i];
        }
    }
};
 
int main()
{
    freopen("gcj_bad_horse_small-practice-2.in", "r", stdin);
    freopen("output.out", "w", stdout);
 
    int input_count;
    cin >> input_count;
 
    int counter = 1;
    while(input_count)
    {
        //do work
        int M;
        cin >> M;
 
        Graph g;
        for(int i=0; i<M; i++)
        {
            string str1, str2;
            cin >> str1 >> str2;
            g.addEdge(str1, str2);
        }
         
        cout << "Case #" << counter++ << ": " << (g.isBipartite()?"Yes":"No") << "\n";
 
        //finish work
        input_count--;
    }
 
    return 0;
}

google code jam -- bad horse

标签:des   color   io   os   ar   for   sp   div   art   

原文地址:http://www.cnblogs.com/bianzeming/p/4009911.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!