【题解】codeforce1228 D. Complete Tripartite⭐⭐⭐ 【构造】

codeforce1228 D. Complete Tripartite

You have a simple undirected graph consisting of n vertices and m edges. The graph doesn’t contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.

Let’s make a definition.

Let v1 and v2 be two some nonempty subsets of vertices that do not intersect. Let f(v1,v2) be true if and only if all the conditions are satisfied:

There are no edges with both endpoints in vertex set v1.
There are no edges with both endpoints in vertex set v2.
For every two vertices x and y such that x is in v1 and y is in v2, there is an edge between x and y.
Create three vertex sets (v1, v2, v3) which satisfy the conditions below;

All vertex sets should not be empty.
Each vertex should be assigned to only one vertex set.
f(v1,v2), f(v2,v3), f(v3,v1) are all true.
Is it possible to create such three vertex sets? If it’s possible, print matching vertex set for each vertex.

Input

The first line contains two integers n and m (3≤n≤105, 0≤m≤min(3⋅105,n(n−1)2)) — the number of vertices and edges in the graph.

The i-th of the next m lines contains two integers ai and bi (1≤ai<bi≤n) — it means there is an edge between ai and bi. The graph doesn’t contain self-loops, there is at most one edge between a pair of vertices. The given graph can be disconnected.

Output

If the answer exists, print n integers. i-th integer means the vertex set number (from 1 to 3) of i-th vertex. Otherwise, print −1.

If there are multiple answers, print any.

Examples

inputCopy
6 11
1 2
1 3
1 4
1 5
1 6
2 4
2 5
2 6
3 4
3 5
3 6
outputCopy
1 2 2 3 3 3
inputCopy
4 6
1 2
1 3
1 4
2 3
2 4
3 4
outputCopy
-1




题意:

给出一个没有重边, 自环的无向图, 问是否能将所有点分为3个集合, 每个集合的点相互互不相连且集合中任一点与其他所有点相连, 即要求划分三分图

题解:

思路当然就是先假设一种分法, 然后再来判断能否这样分

  1. 将同第一个点所有不相连的点归入集合1
  2. 将与第一个点相连的第一个点所有不相连的点归入集合2
  3. 其他点归入集合3

接下来是判断, 满足以下任意情况即输出-1
4. 存在空集
5. 某集合内的任意两点相连
6. 某集合内的任意点没有与其他集合的所有点相连


#include<bits/stdc++.h>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef  long long LL;
const int inf = 1 << 30;
const int maxn = 1e5+10;

int N, M, id[maxn];
vector<int> G[maxn], res[3];
bool vis[maxn];
map<int, int> ma;
int main() {
    int u, v;
    cin >> N >> M;
    if(M < N){  //剪去边不够的情况
        cout << -1 << endl;
        return 0;
    }
    while(M--){
        cin >> u >> v;
        G[u].push_back(v);
        G[v].push_back(u);
    }
    //假设划分3组点
    for(int i = 0; i < G[1].size(); ++i)
        vis[G[1][i]] = true;
    for(int i = 1; i <= N; ++i)
        if(!vis[i])
            id[i] = 1, res[0].push_back(i);
    ms(vis, 0);
    if(!G[1].empty()) u = G[1][0];
    for(int i = 0; i < G[u].size(); ++i)
        vis[G[u][i]] = true;
    for(int i = 1; i <= N; ++i)
        if(!vis[i])
            id[i] = 2, res[1].push_back(i);
    for(int i = 1; i <= N; ++i)
        if(!id[i])
            id[i] = 3, res[2].push_back(i);
    //判断是否可以划分
    bool flag = true;
    if(res[0].empty() || res[1].empty() || res[2].empty()){
        flag = false;
        goto END;
    }
    //判断是否包含自身集合和其他集合所有点
    for(int i = 0; i < 3; ++i){
        ma.clear();
        for(int j = 0; j < res[i].size(); ++j){
            int u = res[i][j];
            for(int k = 0; k < G[u].size(); ++k)
                ++ma[G[u][k]];
        }
        for(int j = 1; j <= N; ++j)
            if((id[j]==i+1 && ma[j]>0) || (id[j]!=i+1 && ma[j]!=res[i].size() )){
                flag = false;
                goto END;
            }
    }
END:if(flag)
        for(int i = 1; i <= N; ++i)
            cout << id[i] << ' ';
    else
        cout << -1;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值