POJ 3207 Ikki's Story IV - Panda's Trick
Ikki's Story IV - Panda's Trick
64-bit integer IO format: %lld Java class name: Main
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 “<code>panda is telling the truth...” or “<code>the evil panda is lying again”.
Sample Input
4 2 0 1 3 2
Sample Output
panda is telling the truth...
Source
解题:传说中的2-SAT...
已知一个圆上顺时针放着 n 个点,这 n 个点中有m对顶点之间有连线,连线要么在园外要么在圆内,每个点最多连接一条边,问是否存在一种连接情况满足所有的边都不相交。
分析:将每条边看成两个点 i ,i+m 分别表示边在内部和在外部, 如果两条边i,j的端点存在序号上的交叉,则这两对点之间的连线一个在外部一个在内部,即如果存在 i,则必存在 j+m ,如果存在 j ,则必存在 i+m, 如果存在 i+m,则必存在 j ,如果存在 j+m ,则必存在 i,建图的时候连的边为
i -> j+m
j -> i+m
i+m -> j
j+m -> i
求出强连通分量并染色,判断是否存在冲突的情况即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1100; 18 int u[maxn],v[maxn],head[maxn],tot,scc,cnt,n,m; 19 int dfn[maxn],low[maxn],belong[maxn]; 20 bool instack[maxn]; 21 stack<int>stk; 22 struct arc { 23 int to,next; 24 arc(int x = 0,int y = 0) { 25 to = x; 26 next = y; 27 } 28 }; 29 arc e[1000010]; 30 void add(int u,int v) { 31 e[tot] = arc(v,head[u]); 32 head[u] = tot++; 33 } 34 bool ok(int i,int j) { 35 return u[i]<v[j]&&u[i]>u[j]&&v[i]>v[j]||v[i]>u[j]&&v[i]<v[j]&&u[i]<u[j]; 36 } 37 void init() { 38 tot = scc = cnt = 0; 39 memset(head,-1,sizeof(head)); 40 for(int i = 0; i < m; i++) 41 for(int j = i+1; j < m; j++) 42 if(ok(i,j)) { 43 add(i,j+m); 44 add(j,i+m); 45 add(j+m,i); 46 add(i+m,j); 47 } 48 memset(dfn,0,sizeof(dfn)); 49 memset(instack,false,sizeof(instack)); 50 while(!stk.empty()) stk.pop(); 51 } 52 void tarjan(int u){ 53 dfn[u] = low[u] = ++cnt; 54 stk.push(u); 55 instack[u] = true; 56 for(int i = head[u]; ~i; i = e[i].next){ 57 if(!dfn[e[i].to]){ 58 tarjan(e[i].to); 59 if(low[e[i].to] < low[u]) 60 low[u] = low[e[i].to]; 61 }else if(instack[e[i].to] && dfn[e[i].to] < low[u]) 62 low[u] = dfn[e[i].to]; 63 64 } 65 if(dfn[u] == low[u]){ 66 scc++; 67 int now; 68 do{ 69 now = stk.top(); 70 stk.pop(); 71 instack[now] = false; 72 belong[now] = scc; 73 }while(now != u); 74 } 75 } 76 bool solve(){ 77 for(int i = 0,t = m<<1; i < t; i++) 78 if(!dfn[i]) tarjan(i); 79 for(int i = 0; i < m; i++) 80 if(belong[i] == belong[i+m]) return false; 81 return true; 82 } 83 int main() { 84 while(~scanf("%d %d",&n,&m)){ 85 for(int i = 0; i < m; i++){ 86 scanf("%d %d",u+i,v+i); 87 if(u[i] > v[i]) swap(u[i],v[i]); 88 } 89 init(); 90 solve()?puts("panda is telling the truth..."):puts("the evil panda is lying again"); 91 } 92 return 0; 93 }
相关文章
- hexcode of é î Latin-1 Supplement
- POJ 2449 Remmarguts' Date
- react native使用 mobx , can't find variable:Symbol
- mysql (已解决)Access denied for user 'root'@'localhost' (using password: NO)
- JAVA-找不到元素 'beans' 的声明
- POJ 2411 Mondriaan's Dream (状压DP)
- POJ 3320 Jessica's Reading Problem (滑动窗口)
- HDU 1756 Cupid's Arrow (几何问题,判定点在多边形内部)
- Error writing file '/tmp/MLLGyECY' (Errcode: 28 - No space left on device)
- 【hihocoder 1424】 Asa's Chess Problem(有源汇上下界网络流)
- Tomcat_启动多个tomcat时,会报StandardServer.await: Invalid command '' received错误
- 源码分析 There is no getter for property named '*' in 'class java.lang.String
- POJ 2528 Mayor's posters
- uwsgi启动报错WARNING: Can't find section "uwsgi" in INI configuration file autotestsite_uwsgi.ini
- hdu 4337 King Arthur's Knights (Hamilton)
- EF:Invalid column name 'Discriminator'.
- Dell'Oro预测:2020年5G RAN收入将远超2010年4G RAN收入水平
- PHP Warning: date() [function.date]: It is not safe to rely on the system's timezone
- AttributeError: module 'torch._C' has no attribute '_cuda_setDevice'(在python命令后面加上 --gpu_ids -1)