【Codeforces Round #589 (Div. 2) D】Complete Tripartite

[Link] I am a link, I point it :) [title] Italy


The meaning of problems

【answer】


This question is actually feeling a little dog.
Thinking something like this
let all the points in a collection.
Then pick a point x, y of an access to its
apparent tag [y] = 2
because he is connected to the incorrect and
then connected to the other point does not clearly and x can be in the same x and set
it to other set points 1 you will find that you want to change it can not change, even if they may have together is useless, because you can not then the other 2 into 1, because you can and before the election of the a conflict (and that you want
2 is not connected to the side of change)
this is the main theme of this ideal is to seize this fuss.
Then then, we are still just looking for a No. 2 inside the collection points a, b runs through its out-degree
Obviously b 3 can be placed only in the collection.
Then still, you will find that you can not move to No. 3 in the collection of the elements.
It's that locked up. . .
Then to verify whether the collection formed to meet the problems in the relationship is like.
To remember to judge | SET1 SET2 | + | SET1 SET3 | + | SET2 SET3 | m is not equal to
if not directly, then determine if this violence and three add up to a lot of words (because you certainly have to judge such violence), will timeout.
(if it is equal to m, m you see in question is relatively small <= 3
10 ^ 5, so no problem of violence determination)

[Code]

#include <bits/stdc++.h>
using namespace std;

const int N = 1e5;

int n,m;
int ans[N+10];
vector<int> h[4];
vector<int> g[N+10];
set<pair<int,int> > myset;

bool ok(int k1,int k2){
    int len1 = h[k1].size();
    int len2 = h[k2].size();
    for (int i = 0;i < len1;i++)
        for (int j = 0;j < len2;j++){
            int x = h[k1][i],y = h[k2][j];
            if (myset.find(make_pair(x,y))==myset.end()) return false;
        }
    return true;
}

int main(){
    scanf("%d%d",&n,&m);
    for (int i = 1;i <= m;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        g[x].push_back(y);g[y].push_back(x);
    }
    for (int i = 1;i <= n;i++){
        ans[i] = 1;
    }
    int len = g[1].size();
    for (int i = 0;i < len;i++){
        int y = g[1][i];
        ans[y] = 2;
    }
    int z = -1;
    for (int i = 1;i <= n;i++)
        if (ans[i]==2){
            z = i;
            break;
        }
    if (z==-1){
        printf("-1");
        return 0;
    }
    len = g[z].size();
    for (int i = 0;i < len;i++){
        int y = g[z][i];
        if (ans[y]==2){
            ans[y] = 3;
        }
    }
    for (int i = 1;i <= n;i++) h[ans[i]].push_back(i);
    if (h[3].empty()) {
        printf("-1");
        return 0;
    }
    int n1 = h[1].size(),n2 = h[2].size(),n3 = h[3].size();
    if (n1*n2+n1*n3+n2*n3!=m){
        printf("-1");
        return 0;
    }
    for (int i = 1;i <= n;i++){
        len = g[i].size();
        for (int j = 0;j < len;j++){
            int y = g[i][j];
            myset.insert(make_pair(i,y));
        }
    }
    if (ok(1,2) && ok(1,3) && ok(2,3)){

    }else{
        printf("-1");
        return 0;
    }
    for (int i = 1;i <= n;i++){
        printf("%d\n",ans[i]);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/AWCXV/p/11612691.html