Relation(NOIP模拟赛)(二分图染色)
原题:
Description
有n个人,编号为1àn,告诉你那些人之间是不友好的。现在,让你将这n个人分成两组,使得每一组之内的人是互相友好的,如果可以分成两组,则输出如何分组的,如果不可以分成两组,那么,输出“IMPOSSIBLE”。
Input
第一行两个整数n和m,1<=n<=50000,0<=m<=500000,分别表示人数以及不友好的人的对数。以下m行每行两个数a,b,表示a与b是不友好的。
Output
如果可以分成两个组,则输出一个方案,第一行为第一组的人的编号,第二行为第二组人的,按升序输出。其中,方案的字典序必须要最小,所谓的方案字典序最小,就是第一行和第二行连接,组成的序列的字典序最小,如果有多种分配方法,取第一个组别人数最多的方案。如果不能分成两组,输出“IMPOSSIBLE”。
Sample Input(relation.in)
5 4
1 4
1 5
2 4
2 5
Sample Output(relation.out)
1 2 3
4 5
样例解释:
有两种方案,1 2 3 和 1 2
4 5 3 4 5
两种方案的字典序相同,取第一组人数最多的方案,所以输出方案一。
题目满足:
40%的数据n<=1000
100%的数据n<=50000,m<=500000
时限 1s
这道题典型的二分图染色(因为分成2组)
当然,根据题目,我们最后要SORT一下输出
并且,方案字典序相同时,取第一组人数最多的方案
所以染色的时候尽量多分配1,不行再回溯修改
下面贴代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct edge{
int to,next;
}g[];
int color[];
int head[],n,m;
bool visit[];
int ans1[],ans2[],num=,num1=,num2=;
void ins(int x,int y){g[++num].next=head[x];head[x]=num;g[num].to=y;}
void insw(int x,int y){ins(x,y);ins(y,x);}
bool dfs(int x){
visit[x]=true;
for(int i=head[x];i;i=g[i].next)
{
if(!visit[g[i].to])
{
color[g[i].to]=color[x]==?:;
visit[g[i].to]=true;
if(!dfs(g[i].to))color[x]=color[x]==?:;
}
else if(color[g[i].to]!=-&&color[g[i].to]==color[x]){color[x]=color[x]==?:;return false;}
}
return true;
}
int main(){
freopen("relation.in","r",stdin);
freopen("relation.out","w",stdout);
memset(color,-,sizeof(color));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
insw(x,y);
}
bool shuchu=false;
for(int i=;i<=n;i++)
{
if(color[i]==-){color[i]=;if(!dfs(i)){printf("IMPOSSIBLE\n");shuchu=true;break;}}
}
if(!shuchu){
for(int i=;i<=n;i++)
if(color[i]||color[i]==-)ans1[++num1]=i; else ans2[++num2]=i;
sort(ans1+,ans1+num1+);
sort(ans2+,ans2+num2+);
for(int i=;i<num1;i++)
printf("%d ",ans1[i]);
printf("%d\n",ans1[num1]);
for(int i=;i<num2;i++)
printf("%d ",ans2[i]);
printf("%d\n",ans2[num2]);
}
fclose(stdin);
fclose(stdout);
}
Relation(NOIP模拟赛)(二分图染色)的更多相关文章
- NOI.AC NOIP模拟赛 第三场 补记
NOI.AC NOIP模拟赛 第三场 补记 列队 题目大意: 给定一个\(n\times m(n,m\le1000)\)的矩阵,每个格子上有一个数\(w_{i,j}\).保证\(w_{i,j}\)互不 ...
- NOIP模拟赛20161022
NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...
- contesthunter暑假NOIP模拟赛第一场题解
contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...
- NOIP模拟赛 by hzwer
2015年10月04日NOIP模拟赛 by hzwer (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...
- 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程
数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...
- 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...
- 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...
- 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1
题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...
- CH Round #58 - OrzCC杯noip模拟赛day2
A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...
- CH Round #52 - Thinking Bear #1 (NOIP模拟赛)
A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...
随机推荐
- 读懂CCS链接命令文件(.cmd)
链接器的核心工作就是符号表解析和重定位,链接命令文件则使得编程者可以给链接器提供必要的指导和辅助信息.多数时候,由于集成开发环境的存在,开发者无需了解链接命令文件的编写,使用默认配置即可.但若需要对计 ...
- ABAP CDS - SELECT, association
ABAP CDS - SELECT, association Syntax ... ASSOCIATION [ [min..max] ] TO target [AS _assoc] ON cond_e ...
- Trident整合Kafka
首先编写一个打印函数KafkaPrintFunction import org.apache.storm.trident.operation.BaseFunction; import org.apac ...
- string函数Contains()实例
public bool Contains(string value)如果值参数出现在此字符串内,或者值为空字符串(“”),则为true; 否则为false using System; class Ex ...
- Win Server 8中的利器:微软在线备份服务
微软在Windows Server 8中添加在线备份服务了?你一定以为我在开玩笑,是吧?但是微软确实这么做了. 微软在Windows Server 8中添加在线备份服务了?你一定以为我在开玩笑 ...
- 将有效慢日志转存到数据库v2
import re import sys import getopt import MySQLdb from subprocess import call import os host='10.76. ...
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目7
2014-04-24 22:06 题目:搜索引擎问题,如果有一列100台服务器的集群,用来响应查询请求,你要如何设计query分发和cache策略? 解法:query分发可以用计算数字签名并对机器数取 ...
- php jsonp单引号转义
php中jsonp输出时一般用下面的格式: callbackname('json string'); 如果中间的json string中含有单引号,这个输出就是有问题的,调用方一般是无法处理的,所以我 ...
- python-isinstance,issubclass
1 #当存在继承关系时,两个类中存在相同的方法,如何执行父类的方法,通过super 2 class C1: 3 def f1(self): 4 print('c1.f1') 5 6 7 class C ...
- ironic rescue standard rescue and unrescue process
翻译官网救援/取消救援标准流程 1.用户在节点上调用Nova rescue 2.Nova ComputeManager调用virt驱动程序的rescue()方法,传入rescue_password作为 ...