google code jam -- bad horse

二分图解决这个问题的思路

#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;

}

时间: 2024-10-22 03:56:18

google code jam -- bad horse的相关文章

Google Code Jam 2014 Round2

Google Code Jam Round2 晚上10点开始的,开始google一直上不去,然后开了个vpn就行了. 说说我的情况吧. P1就是某年noip普及组纪念品分组,贪心. p2是说给你一个排列,然后每次可以交换两个相邻的数,使得最后序列变为先上升后下降的样子.(n<=1000) 一开始题目看错了,以为是交换任意两个,然后愣了半天不会,去写后两题暴力了. (话说我Round1Cp1题目也看错了害的我逗了好久) 后来突然发现是adjacent elements...(<论英语学习的重要性

Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. Cells number i and i+1 are adjacent, and prisoners in adjacent cells are called "neighbours." A wall with a window separates adjacent cells, and

Google Code Jam 2012 Practice - Store Credit

Problem You receive a credit C at a local store and would like to buy two items. You first walk through the store and create a list L of all available items. From this list you would like to buy two items that add up to the entire value of the credit

Java学习笔记(5)----使用正则表达式解决Google Code Jam Qualification2009赛题 Alien Language

原题地址:https://code.google.com/codejam/contest/90101/dashboard#s=p0 题目描述: Problem After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word

Google Code Jam 2014 Round 2回顾和分析

回顾 比赛开始网络就一直在抽风,不知道宿舍网渣还是有人攻击服务器.刷了n遍n久刷出了题目.提交A小case的时候眼睁睁看着时间过去,却提交不上,这破网.最后A题B题还是解决了,C题扫了一眼,读都没读,就奔D题去了,因为我看到了熟悉的trie这个单词,加之看到小case只有9分,判断小case应该比较容易.前面因为网络浪费了很多时间,加之从未编过trie的程序,只能临时上网翻书去学,最后D小这个可以很容易暴力解的问题也没编完. 最终的rank是13xx,考虑到在这次GCJ之前从未接触过编程竞赛,而

Google Code Jam在线测试题目--Alien Language

Problem After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words

Google Code Jam在线測试题目--Alien Language

Problem After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words

Google Code Jam Round 1A 2015 Problem B. Haircut 二分

Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barbers on duty, and they are numbered 1 through B. It always takes the kth barber exactly Mk minutes to cut a customer's hair, and a barber can only cut

Google Code Jam 2014 Round 1B Problem B

二进制数位DP,涉及到数字的按位与操作. 查看官方解题报告 #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> using namespace std; #define MAX_LEN 50 long long A, B, K; int a[MAX_LEN], b[MAX_LEN], k[MAX_LEN]; long long memoize[MAX_LEN]