【2-SAT】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
先反复地扫(不超过n次),把所有可以确定唯一取法的给确定下来。
然后对于剩下的不能确定的,跑2-SAT。输出可行解时,对于a和¬a,如果a所在的强连通分量序号在¬a之前,则取a,否则不取a。如果a和¬a在同一个强连通分量,则无解。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cstring>
using namespace std;
struct data
{
int x1,x2,id;
string s1,s2;
};
data a[1010];
string s1,s2,anss[1010];
int cho[1010];
bool S[100010];
bool __cmp(const data &a,const data &b)
{
return a.x1<b.x1;
}
int n;
vector<int>G[40010],rG[40010],vs;
bool used[40010],will[20010];
int cmp[40010];
void AddEdge(int U,int V)
{
G[U].push_back(V);
rG[V].push_back(U);
}
void dfs(int U)
{
used[U]=1;
for(int i=0;i<G[U].size();++i)
if(!used[G[U][i]])
dfs(G[U][i]);
vs.push_back(U);
}
void rdfs(int U,int k)
{
used[U]=1;
cmp[U]=k;
for(int i=0;i<rG[U].size();++i)
if(!used[rG[U][i]])
rdfs(rG[U][i],k);
}
bool cho2[20010];
int main()
{
// freopen("d.in","r",stdin);
scanf("%d",&n);
for(int i=1;i<=n;++i)
{
cin>>s1>>s2;
a[i].s1=s1.substr(0,3);
a[i].s2=s1.substr(0,2)+s2.substr(0,1);
a[i].x1=(s1[0]-'A')*26*26+(s1[1]-'A')*26+s1[2]-'A';
a[i].x2=(s1[0]-'A')*26*26+(s1[1]-'A')*26+s2[0]-'A';
a[i].id=i;
}
sort(a,a+1+n,__cmp);
int sta;
for(int i=1;i<=n;++i)
{
if(a[i].x1!=a[i-1].x1) sta=i;
if(a[i].x1!=a[i+1].x1 && i>sta)
{
for(int j=sta;j<=i;++j)
{
cho[j]=2;
if(S[a[j].x2])
{
puts("NO");
return 0;
}
S[a[j].x2]=1;
}
}
}
while(1)
{
bool flag=0;
for(int i=1;i<=n;++i)
if(!cho[i])
{
if(S[a[i].x1] && S[a[i].x2])
{
puts("NO");
return 0;
}
else if(S[a[i].x1])
{
flag=1;
cho[i]=2;
S[a[i].x2]=1;
}
else if(S[a[i].x2])
{
flag=1;
cho[i]=1;
S[a[i].x1]=1;
}
else if(a[i].x1==a[i].x2)
{
cho[i]=1;
flag=1;
S[a[i].x1]=1;
}
}
if(!flag)
break;
}
for(int i=1;i<=n;++i)
if(!cho[i])
{
will[a[i].x1]=will[a[i].x2]=1;
AddEdge(a[i].x1,a[i].x2+20000);
AddEdge(a[i].x2,a[i].x1+20000);
}
for(int i=1;i<=20000;++i) if(will[i])
if(!used[i])
dfs(i);
memset(used,0,sizeof(used));
int cnt=0;
for(int i=vs.size()-1;i>=0;--i)
if(!used[vs[i]])
rdfs(vs[i],++cnt);
for(int i=1;i<=20000;++i) if(will[i])
if(cmp[i]==cmp[i+20000])
{
puts("NO");
return 0;
}
else if(cmp[i]>cmp[i+20000])
cho2[i]=1;
for(int i=1;i<=n;++i)
if(cho[i]==1)
anss[a[i].id]=a[i].s1;
else if(cho[i]==2)
anss[a[i].id]=a[i].s2;
else if(cho2[a[i].x1])
anss[a[i].id]=a[i].s1;
else
anss[a[i].id]=a[i].s2;
puts("YES");
for(int i=1;i<=n;++i)
cout<<anss[i]<<endl;
return 0;
}
【2-SAT】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League的更多相关文章
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D. Innokenty and a Football League
地址:http://codeforces.com/contest/782/problem/D 题目: D. Innokenty and a Football League time limit per ...
- 【贪心】【DFS】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) C. Andryusha and Colored Balloons
从任意点出发,贪心染色即可. #include<cstdio> #include<algorithm> using namespace std; int v[200010< ...
- 【三分】Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) B. The Meeting Place Cannot Be Changed
三分显然,要注意EPS必须设成1e-6,设得再小一点都会TLE……坑炸了 #include<cstdio> #include<algorithm> #include<cm ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)
Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals )D. Innokenty and a Football League(2-sat)
D. Innokenty and a Football League time limit per test 2 seconds memory limit per test 256 megabytes ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)【A,B,C】
翻车!翻车! codeforces782A A题: 水. 代码: #include <bits/stdc++.h> using namespace std; typedef long lo ...
- 树的性质和dfs的性质 Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E
http://codeforces.com/contest/782/problem/E 题目大意: 有n个节点,m条边,k个人,k个人中每个人都可以从任意起点开始走(2*n)/k步,且这个步数是向上取 ...
- 2-sat Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) D
http://codeforces.com/contest/782/problem/D 题意: 每个队有两种队名,问有没有满足以下两个条件的命名方法: ①任意两个队的名字不相同. ②若某个队 A 选用 ...
- Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab
地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...
随机推荐
- PowerShell官方文档
PowerShell PowerShell 在 .NET Framework 基础之上构建,是一种基于任务的命令行 Shell 脚本语言:专门面向系统管理员和高级用户,可快速自动化多个操作系统(Lin ...
- javascript中Date使用总结(转)
//全局函数 Date //Date 类的静态方法 Date.parse Date.UTC //Date 对象的建立方法 new Date() new Date(毫秒数) new Date(标准时间格 ...
- js密码的匹配正则
匹配的密码是 数字大写或者小写的字母.符号. if(pwd.match(/[\d]/) && pwd.match(/[A-Za-z]/) && pwd.match(/[ ...
- oracle中分页的知识
一:前言 自从出来实习后,基本上都没有按下心来总结下自己学的知识点,刚刚好现在快要国庆了,没有到深圳出差,在公司呆了三天,可以说是在公司打了三天的酱油啊,所以前两天都是在看些正则的文档,并且写了下总结 ...
- HDU2553 N皇后问题---(dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=2553 在N*N的方格棋盘放置了N个皇后,使得它们不相互攻击(即任意2个皇后不允许处在同一排,同一列,也不允许处在 ...
- bzoj 1576: [Usaco2009 Jan]安全路经Travel——并查集+dijkstra
Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, b_i,和t_i Output * 第1..N-1行: 第i行包含一个数 ...
- sublime text 2学习(二):创建可复用的代码片段
对于前端工程师来讲,写一个html页面的基本结构是体力活,每次去拷贝一个也麻烦,sublime text 2 提供了一个很好的复用代码片段.下面介绍一下创建一个html5的代码片段的过程. 在菜单上点 ...
- LeetCode 4 :Majority Element
problem:Given an array of size n, find the majority element. The majority element is the element tha ...
- POJ2479(最长连续子序列和)
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37035 Accepted: 11551 Des ...
- mysqlalchmy操作之建表
1 创建链接基础类. # -*- coding=utf-8 -*- import os from sqlalchemy import (create_engine,MetaData) from sql ...