锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Description 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 “ Sample Input Sample Outputpanda is telling the truth...” or “the evil panda is lying again”.4 2
0 1
3 2panda is telling the truth...
棰樻剰:緇欏嚭n涓瀯鎴愪竴涓幆鐨勭偣浠ュ強鐐硅窡鐐圭殑榪炴帴鍏崇郴銆傝姹傝竟涓嶈兘鐩鎬氦銆傛瘮濡傦細鏈変竴鏉¤竟i浼氳窡j鐪嬫垚鐩寸嚎鐨勮瘽鍦ㄥ洯鍐呯浉浜わ紝閭d箞鍙互
鎶婁竴鏉¢氳繃杈瑰集緇曡繃鍦嗭紝浣垮緱涓ゆ潯杈逛笉鐩鎬氦銆傛渶鍚庨棶鏄惁鎵鏈夌嚎孌甸兘涓嶇浉浜ゃ?br>鍒嗘瀽錛氬浜庝袱鏉¤竟錛屽鏋滃湪鍥唴鐩鎬氦錛屽垯蹇呴』寮曞涓鏉¢氳繃鍥銆傛妸杈筰鐪嬫垚鐐筰1鍜宨2鍒嗗埆琛ㄧず閫氳繃鍥唴鍜屽洯澶栥?br>濡傛灉i涓巎鐩鎬氦浜庡洯鍐咃紝鍒檌1->j2錛宨2->j1, j2->i1, j1->i2.琛ㄧず涓ゆ潯杈逛竴鏉″湪鍥錛屼竴鏉″湪鍥唴銆傞氳繃姹俿cc紜畾姣忔潯杈圭殑i1鍜宨2
鎵鍦ㄧ殑scc鏄惁鐩稿悓錛岀浉鍚岃〃紺篿鏃㈡槸鍦ㄥ洯鍐呬篃鏄湪鍥錛岀煕鐩句簡銆?br>浠g爜錛?br>
#include <stdlib.h>
#define Min(a, b) a < b ? a : b
#define maxn 5010
struct L
{
int x, y;
}line[500000];
struct g
{
int v, next;
}fn[maxn * 4];
int g[maxn * 5], visit[maxn * 5], low[maxn * 5];
void set(int m)
{
for (int i = 1; i <= m; i++)
{
g[i] = -1, visit[i] = 0;
}
}
int tarjan(int u, int f, int times)
{
low[u] = times;
visit[u] = 1;
int i, v;
for (i = g[u]; i != -1; i = fn[i].next)
{
v = fn[i].v;
if (v != f)
{
if (!visit[v])
{
times = tarjan(v, u, times + 1);
}
low[u] = Min(low[u], low[v]);
}
}
return times;
}
int main()
{
int n, m, i, j, th, times;
scanf("%d%d", &n, &m);
set(m * 2);
for (i = 1; i <= m; i++)
{
scanf("%d%d", &line[i].x, &line[i].y);
if (line[i].x > line[i].y)
{
line[i].x ^= line[i].y, line[i].y ^= line[i].x, line[i].x ^= line[i].y;
}
}
for (i = 1, th = 0; i < m; i++)
{
for (j = i + 1; j <= m; j++)
{
//瀵圭浉浜ょ殑綰挎寤哄浘
if (line[i].x < line[j].y && line[i].x > line[j].x && line[i].y > line[j].y
|| line[i].y < line[j].y && line[i].y > line[j].x && line[i].x < line[j].x)
{
//printf("%d %d %d %d\n", line[i].x, line[i].y, line[j].x, line[j].y);
fn[th].v = j + m, fn[th].next = g[i], g[i] = th++;
fn[th].v = i, fn[th].next = g[j+m], g[j+m] = th++;
fn[th].v = j, fn[th].next = g[i+m], g[i+m] = th++;
fn[th].v = i + m, fn[th].next = g[j], g[j] = th++;
}
}
}//system("pause");
for (i = 1, times = 1; i <= 2 * m; i++)
{
if (!visit[i])
{
times = tarjan(i, -1, times + 1);
}
//printf("[%d]=%d ", i, low[i]);
}//printf("\n");
for (i = 1; i <= m; i++)
{
if (low[i] == low[i+m])
{
break;
}
}
if (i == m + 1)
{
printf("panda is telling the truth
\n");
}
else
{
printf("the evil panda is lying again\n");
}
system("pause");
return 0;
}
/*
10 3
1 5
2 6
7 3
*/
]]>