liympanda, one of Ikki’s friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many times, he is tired of such easy games and wants to play another game with Ikki.
liympanda has a magic circle and he puts it on a plane, there are n points on its boundary in circular border: 0, 1, 2, …, n − 1. Evil panda claims that he is connecting m pairs of points. To connect two points, liympanda either places the link entirely inside the circle or entirely outside the circle. Now liympanda tells Ikki no two links touch inside/outside the circle, except on the boundary. He wants Ikki to figure out whether this is possible…
Despaired at the minesweeping game just played, Ikki is totally at a loss, so he decides to write a program to help him.
Input
The input contains exactly one test case.
In the test case there will be a line consisting of of two integers: n and m (n ≤ 1,000, m ≤ 500). The following m lines each contain two integers ai and bi, which denote the endpoints of the ith wire. Every point will have at most one link.
Output
Output a line, either “panda is telling the truth…” or “the evil panda is lying again”.
Sample Input
4 2
0 1
3 2
Sample Output
panda is telling the truth…
当两条边出现这种情况的时候,这个时候两条边就必须一条在外面,一条放在圆的里面。
我们把每一条边都看出一个点,如果两边相互矛盾,就对这两个点连线,很明显,相邻的两个点不可能染同一种颜色,就直接二分图染色。
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
bool check(int x,int y,int z,int t){
if(z>x&&z<y&&t>y)return true;
if(t>x&&t<y&&z<x)return true;
return false;
}
const int N=5e2+10;
int n,m;
vector<int>v[N];
int col[N];
struct e
{
int a,b;
}endge[N];
bool dfs(int x)
{
for(int i=0;i<int(v[x].size());i++){
int nw=v[x][i];
if(col[nw]==-1){
col[nw]=col[x]^1;
dfs(nw);
}
else{
if(col[nw]==col[x])return false;
}
}
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
int a,b;
for(int i=1;i<=m;i++){
cin>>a>>b;
endge[i].a=min(a,b);
endge[i].b=max(a,b);
}
for(int i=1;i<m;i++){
for(int j=i+1;j<=m;j++){
if(check(endge[i].a,endge[i].b,endge[j].a,endge[j].b)){
v[i].push_back(j);
v[j].push_back(i);
}
}
}
memset(col,-1,sizeof(col));
for(int i=1;i<=m;i++){
if(col[i]==-1){
col[i]=0;
if(!dfs(i)){
cout<<"the evil panda is lying again"<<endl;return 0;
}
}
}
cout<<"panda is telling the truth..."<<endl;
return 0;
}
半年前学的2-sat了,复习下模板!
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
bool check(int x,int y,int z,int t){
if(z>x&&z<y&&t>y)return true;
if(t>x&&t<y&&z<x)return true;
return false;
}
const int N=1e3+10;
int n,m,p;
int stk[N];
vector<int>v[N];
bool col[N];
struct e
{
int a,b;
}endge[N];
bool dfs(int x)
{
if(col[x^1])return false;
if(col[x])return true;
col[x]=true;
stk[++p]=x;
for(int i=0;i<int(v[x].size());i++){
if(!dfs(v[x][i])){
return false;
}
}
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>m;
int a,b;
for(int i=1;i<=m;i++){
cin>>a>>b;
endge[i].a=min(a,b);
endge[i].b=max(a,b);
}
for(int i=1;i<m;i++){
for(int j=i+1;j<=m;j++){
if(check(endge[i].a,endge[i].b,endge[j].a,endge[j].b)){
v[2*i-2].push_back(2*j-1);
v[2*j-1].push_back(2*i-2);
v[2*i-1].push_back(2*j-2);
v[2*j-2].push_back(2*i-1);
}
}
}
memset(col,false,sizeof(col));
for(int i=0;i<2*m;i+=2){
if(!col[i]&&!col[i+1]){
if(!dfs(i)){
while(p){
col[stk[p]]=false;
p--;
}
if(!dfs(i^1));
cout<<"the evil panda is lying again"<<endl;return 0;
}
}
}
cout<<"panda is telling the truth..."<<endl;
return 0;
}