Codeforces Round #589 (Div. 2)D(思维,构造)

#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
vector<int>adj[100007];
map<vector<int>,int>mp;
int main(){
int n,m;
scanf("%d%d",&n,&m);
int u,v;
for(int i=1;i<=m;++i){
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
int type=0;
for(int i=1;i<=n;++i){
if(!adj[i].size()||type>3){//独立点和其他集合的点之间不存在边或者集合多于3个都是-1情况
printf("-1");
return 0;
}
sort(adj[i].begin(),adj[i].end());//全部排为升序才能mp寻值
if(!mp[adj[i]])//与i相连的这组点第一次出现
mp[adj[i]]=++type;//将它们分到一个集合
}
if(type!=3)
printf("-1");
else
for(int i=1;i<=n;++i)
printf("%d ",mp[adj[i]]);
return 0;
}

原文地址:https://www.cnblogs.com/ldudxy/p/11624002.html

时间: 2024-07-30 14:02:20

Codeforces Round #589 (Div. 2)D(思维,构造)的相关文章

Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理 [Problem Description] 在\(n\times n\)的格子中填入\([1,k]\)之间的数字,并且保证每一行至少有一个\(1\),每一列至少有一个\(1\),问有多少种满足条件的填充方案. [Solution] 令\(R[i]\)表示为第\(i\)行至少有一个\(1\)的方案数,\(C[i]\)表示第\(i\)列至少有一个\(1\)的方案数.则题目要

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,

Codeforces Round #459 (Div. 2) C 思维,贪心 D 记忆化dp

Codeforces Round #459 (Div. 2) C. The Monster 题意:定义正确的括号串,是能够全部匹配的左右括号串. 给出一个字符串,有 (.). ? 三种字符, ? 可以当作 ( 可 ) . 问这个字符串有多少个子串是正确的括号串. tags:好考思维,想不到.. 预处理出每个字符向左向右最多可以匹配到哪里,再 O(n*n) 枚举所有区间,看是否符合条件. // C #include<bits/stdc++.h> using namespace std; #pra

Codeforces Round #143 (Div. 2) (ABCD 思维场)

题目连链接:http://codeforces.com/contest/231 A. Team time limit per test:2 seconds memory limit per test:256 megabytes One day three best friends Petya, Vasya and Tonya decided to form a team and take part in programming contests. Participants are usually

Codeforces Round #589 (Div. 2) (e、f没写)

https://codeforces.com/contest/1228/problem/A A. Distinct Digits 超级简单嘻嘻,给你一个l和r然后寻找一个数,这个数要满足的条件是它的每一位的数字不相同,找出满足要求的最小的那个数输出,没有找到就输出-1: 1 #include<bits/stdc++.h> 2 using namespace std; 3 bool check(int n){ 4 int vis[15]={0}; 5 while(n){ 6 if(!vis[n%

Codeforces Round #276 (Div. 2)C. Bits(构造法)

这道题直接去构造答案即可. 对于l的二进制表示,从右到左一位一位的使其变为1,当不能再变了(再变l就大于r了)时,答案就是l. 这种方法既可以保证答案大于等于l且小于等于r,也可以保证二进制表示时的1最多. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include&l

Codeforces Round #589 (Div. 2) B——B. Filling the Grid

Suppose there is a h×wh×w grid consisting of empty or full cells. Let's make some definitions: riri is the number of consecutive full cells connected to the left side in the ii-th row (1≤i≤h1≤i≤h). In particular, ri=0ri=0 if the leftmost cell of the 

Codeforces Round 589 (Div. 2) 题解

Is that a kind of fetishism? No, he is objectively a god. 见识了一把 Mcdic 究竟出题有多神. (虽然感觉还是吹过头了) 开了场 Virtual 玩. 开场先秒了 AB.C 居然差点没做出来,有点耻辱. 开 D.怎么不会--Div. 2 的 D 都能卡住我,我心态崩了. 调到 E. woc 这不是 sb 题吗-- 回来肝 D.想了想口胡出来了,然而心态已经崩了,用了很长很长时间才打出来. 最后只剩 20min 时开 F.这--辣鸡三合

Codeforces Round #589 (Div. 2) A. Distinct Digits

链接: https://codeforces.com/contest/1228/problem/A 题意: You have two integers l and r. Find an integer x which satisfies the conditions below: l≤x≤r. All digits of x are different. If there are multiple answers, print any of them. 思路: 水题. 代码: #include