题目大意:
  有n个国家要派代表开会,每个国家有两个代表可供选择。
  有m对代表有仇,不能同时开会。
  若每个国家只能派一个代表开会,问是否存在一种方案,使得每个国家都能正常参会?
  如果有,输出字典序最小的一种。

思路:
  2-SAT经典模型。
  如果两个代表之间有仇,那么就给其中一个代表a连一条通向另一国家另一代表b的有向边,表示选a后一定要选b。
  判定时就用Tarjan缩点,如果同一国家两个代表在同一个强连通分量中,那么说明两个代表必须同时参加或不参加,这是不合法的。
  构造字典序最小的方案时,可以从小到大枚举每一个代表,然后DFS选定该代表后,必须选的其它代表,如果必须选的的代表和已选代表矛盾,则不加入。

 #include<stack>
#include<cstdio>
#include<cctype>
#include<vector>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
std::stack<int> s;
std::vector<int> e[N<<];
bool ins[N<<],select[N<<];
int dfn[N<<],low[N<<],scc[N<<],cnt,id;
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
}
void tarjan(const int &x) {
s.push(x);
ins[x]=true;
dfn[x]=low[x]=++cnt;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(!dfn[y]) {
tarjan(y);
low[x]=std::min(low[x],low[y]);
} else if(ins[y]) {
low[x]=std::min(low[x],dfn[y]);
}
}
if(low[x]==dfn[x]) {
id++;
int y=-;
while(y!=x) {
y=s.top();
s.pop();
ins[y]=false;
scc[y]=id;
}
}
}
bool dfs(const int &x) {
if(select[x^]) return false;
s.push(x);
select[x]=true;
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(select[y]) continue;
if(!dfs(y)) return false;
}
return true;
}
inline void reset(const int &n) {
cnt=id=;
for(register int i=;i<n<<;i++) {
dfn[i]=low[i]=select[i]=;
e[i].clear();
}
}
int main() {
int n,m;
while(~scanf("%d%d",&n,&m)) {
while(m--) {
const int u=getint()-,v=getint()-;
add_edge(u,v^);
add_edge(v,u^);
}
for(register int i=;i<n<<;i++) {
if(!dfn[i]) tarjan(i);
}
for(register int i=;i<n;i++) {
if(scc[i<<]==scc[i<<|]) {
puts("NIE");
goto Next;
}
}
for(register int i=;i<n<<;i++) {
if(!select[i]&&!select[i^]) {
if(dfs(i)) {
while(!s.empty()) s.pop();
} else {
while(!s.empty()) {
const int x=s.top();
s.pop();
select[x]=false;
}
}
}
}
for(register int i=;i<n<<;i++) {
if(select[i]) printf("%d\n",i+);
}
Next:
reset(n);
}
return ;
}

[POI2001]Peaceful Commission的更多相关文章

  1. hdu 1814 Peaceful Commission (2-sat 输出字典序最小的路径)

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  2. HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题)

    HDU 1814 Peaceful Commission / HIT 1917 Peaceful Commission /CJOJ 1288 和平委员会(2-sat模板题) Description T ...

  3. Peaceful Commission

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...

  4. hdu1814 Peaceful Commission

    hdu1814 Peaceful Commission 题意:2-sat裸题,打印字典序最小的 我写了三个 染色做法,正解 scc做法,不管字典序 scc做法,错误的字典序贪心 #include &l ...

  5. HDOJ 1814 Peaceful Commission

    经典2sat裸题,dfs的2sat能够方便输出字典序最小的解... Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Mem ...

  6. 图论--2-SAT--HDU/HDOJ 1814 Peaceful Commission

    Peaceful Commission Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. HDU 1814 Peaceful Commission(2-sat 模板题输出最小字典序解决方式)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 Problem Description The Public Peace Commission ...

  8. 【HDU】1814 Peaceful Commission

    http://acm.hdu.edu.cn/showproblem.php?pid=1814 题意:n个2人组,编号分别为2n和2n+1,每个组选一个人出来,且给出m条关系(x,y)使得选了x就不能选 ...

  9. HDU-1814 Peaceful Commission 2sat

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1814 简单的2sat题. //STATUS:C++_AC_390MS_996KB #include & ...

随机推荐

  1. [codechef FNCS]分块处理+树状数组

    题目链接:https://vjudge.net/problem/CodeChef-FNCS 在一个地方卡了一晚上,就是我本来以为用根号n分组,就会分成根号n个.事实上并不是....因为用的是根号n下取 ...

  2. UVA10480:Sabotage(最小割+输出)

    Sabotage 题目链接:https://vjudge.net/problem/UVA-10480 Description: The regime of a small but wealthy di ...

  3. oralce的客户端sqlplus

    安装完oracle后,默认的客户端是sqlplus,还有一个公司常用的是PLSQLdeveloper 客户端软件,另外Navicat primie这个可以连接mysql.sqlserver.oracl ...

  4. Binding and styling text to a RichTextBox in WPF

    http://www.codeproject.com/Articles/137209/Binding-and-styling-text-to-a-RichTextBox-in-WPF The Rich ...

  5. java实现ssm框架的crud

    上一篇博客写了通过表名获取数据库表结构的demo,现在我以此为基础实现了一个简单的通过数据库表结构生成对应的实体,通过读取mapper接口文件.mapping映射文件. service映射文件模板,替 ...

  6. MySQL远程访问权限 允许远程连接

    1 首先cd / 到根目录,打开mysql控制台 登录数据库  mysql -u root -p 2.授权远程连接 mysql> use mysql; Database changed mysq ...

  7. CSS中z-index全解析

    一.z-index解释 z-index属性决定了一个HTML元素的层叠级别,元素层叠级别是相对于元素在Z轴上(与X轴Y轴相对照)的位置而言.一个更高的z-index值意味着这个元素在叠层顺序中会更靠近 ...

  8. [洛谷P1032] 字串变换

    洛谷题目链接:字串变换 题目描述 已知有两个字串 A, B 及一组字串变换的规则(至多6个规则): A1 -> B1 A2 -> B2 规则的含义为:在 A$中的子串 A1 可以变换为 B ...

  9. php多虚拟主机配置

    一.配置httpd.conf# Virtual hosts#Include conf/extra/httpd-vhosts.conf       //取消这一行的# 二.配置httpd-vhosts. ...

  10. bzoj 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居——排序+贪心+set

    Description 了解奶牛们的人都知道,奶牛喜欢成群结队.观察约翰的N(1≤N≤100000)只奶牛,你会发现她们已经结成了几个“群”.每只奶牛在吃草的时候有一个独一无二的位置坐标Xi,Yi(l ...