【codeforce】B. Flag Day

来源:互联网 发布:手机赚钱软件,提现微信 编辑:程序博客网 时间:2024/05/21 09:08

原题:http://codeforces.com/contest/357/problem/B

B. Flag Day
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In Berland, there is the national holiday coming — the Flag Day. In the honor of this event the president of the country decided to make a big dance party and asked your agency to organize it. He has several conditions:

  • overall, there must be m dances;

  • exactly three people must take part in each dance;

  • each dance must have one dancer in white clothes, one dancer in red clothes and one dancer in blue clothes (these are the colors of the national flag of Berland).

    The agency has n dancers, and their number can be less than 3m. That is, some dancers will probably have to dance in more than one dance. All of your dancers must dance on the party. However, if some dance has two or more dancers from a previous dance, then the current dance stops being spectacular. Your agency cannot allow that to happen, so each dance has at most one dancer who has danced in some previous dance.

    You considered all the criteria and made the plan for the m dances: each dance had three dancers participating in it. Your task is to determine the clothes color for each of the ndancers so that the President's third condition fulfilled: each dance must have a dancer in white, a dancer in red and a dancer in blue. The dancers cannot change clothes between the dances.

  • Input

    The first line contains two space-separated integers n (3 ≤ n ≤ 105) and m (1 ≤ m ≤ 105) — the number of dancers and the number of dances, correspondingly. Then m lines follow, describing the dances in the order of dancing them. The i-th line contains three distinct integers — the numbers of the dancers that take part in the i-th dance. The dancers are numbered from 1 to n. Each dancer takes part in at least one dance.

    Output

    Print n space-separated integers: the i-th number must represent the color of the i-th dancer's clothes (1 for white, 2 for red, 3 for blue). If there are multiple valid solutions, print any of them. It is guaranteed that at least one solution exists.

    Sample test(s)
    input
    7 31 2 31 4 54 6 7
    output
    1 2 3 3 2 2 1 
    input
    9 33 6 92 5 81 4 7
    output
    1 1 1 2 2 2 3 3 3 
    input
    5 24 1 53 1 2
    output
    2 3 1 1 3 


    分析:思维题。先给第一组的三个人分别分配1,2,3,然后从第二组开始遍历,如果三个人都没出现过,则按顺序分配1,2,3,如果有一个人出现过,则其他两个人分配与之不同的号码或者成为颜色。因为题目中有限制,“ each dance has at most one dancer who has danced in some previous dance”,每组中不会出现有两个或者以上的人在前面演出中出现。另外,题目中说明有解,因此不用检查最后的分配结果是否合理。


    代码:

    #include <iostream>#include <vector>using namespace std;vector<int> Solution(int num[][3],int n,int m);int main(){    int n,m;    cin >> n >> m;    int num[m][3];    for(int i=0 ; i<m ; i++){        cin >> num[i][0] >> num[i][1] >> num[i][2];    }    vector<int>result = Solution(num,n,m);    for(vector<int>::size_type i=0 ; i!=result.size() ; i++){        cout << result[i] << " ";    }    cout << endl;    return 0;}vector<int> Solution(int num[][3],int n,int m){    vector<int> ret(n+1,0);    ret.at(num[0][0]) = 1;    ret.at(num[0][1]) = 2;    ret.at(num[0][2]) = 3;    for(int i=1 ; i<m ; i++){        if(ret.at(num[i][0]) != 0){            ret.at(num[i][1]) = (ret.at(num[i][0]))%3+1;            ret.at(num[i][2]) = (ret.at(num[i][1]))%3+1;        }        else if(ret.at(num[i][1]) != 0){            ret.at(num[i][0]) = (ret.at(num[i][1]))%3+1;            ret.at(num[i][2]) = (ret.at(num[i][0]))%3+1;        }        else if(ret.at(num[i][2]) != 0){            ret.at(num[i][1]) = (ret.at(num[i][2]))%3+1;            ret.at(num[i][0]) = (ret.at(num[i][1]))%3+1;        }        else{            ret.at(num[i][0]) = 1;            ret.at(num[i][1]) = 2;            ret.at(num[i][2]) = 3;        }    }    ret.erase(ret.begin());    return ret;}


    总结:1.开始时总想分配时要不要按指定顺序,即编号最小的分配1,最大的分配3,但后来发现顺序无关紧要,最多只是三种颜色的人相互交换下颜色而已。

    2.在分配给第二组和以后各组颜色时,在想要不要考虑每组中已分配的人数,例如0人,一人,两人或者三人,这样的话,情况会很多(8种),写代码时很复杂。后来再看一遍题之后,发现最多后面两种情况不可能,因此只会有四种情况。仔细看题!!

    3.在具体分配时,开始用的代码是ret.at(num[i][1]) = (ret.at(num[i][0])+1)%3;这样会出现0代替3的情况,然后纠正后改为ret.at(num[i][1]) = (ret.at(num[i][0]))%3+1;就符合要求了。

    没有高深的算法,还是锻炼思维。

    原创粉丝点击