Description
Take any directed graph D with n vertices and m edges. You can make the Lying graph E of B in
the following way. E will have m vertices, one for each edge of D. For example, if D has an edge uv,
then E will have a vertex called uv. Now, whenever D has edges uv and vw, E will have an edge from
vertex uv to vertex vw. There are no other edges in E.
You will be given a graph E and will have to determine whether it is possible for E to be the Lying
graph of some directed graph D.
Input
The first line of input gives the number of cases, N (N < 220). N test cases follow. Each one starts
with two lines containing m (0 ≤ m ≤ 300) and k. The next k lines will each contain a pair of vertices,
x and y, meaning that there is an edge from x to y in E. The vertices are numbered from 0 to m − 1
Output
For each test case, output one line containing ‘Case #x:’ followed by either ‘Yes’ or ‘No’, depending on
whether E is a valid Lying graph or not. Note that D is allowed to have duplicate edges and self-edges.
Simple Input
4
2
1
0 1
5
0
4
3
0 1
2 1
2 3
3
9
0 1
0 2
1 2
1 0
2 0
2 1
0 0
1 1
2 2
Simple Output
Case #1: Yes
Case #2: Yes
Case #3: No
Case #4: Yes
题目大意:D为有向图,D的每条边对应E的一个结点,若D有边uv,则E有结点uv,对于D的两条边uv和vw,E中的两个结点uv与vw连边。给出E,判断是否有对应的D。
思路:若对于a,b,c,d,e五个结点,D中有ab,bc,cd,de四条,则在E中有ab->cd,ab->de,bc->cd,bc->四条边。如果对于x和y到k1都有边,x和y只有其中一个到k2有边,则无法转化。cout迷之WA,换printf AC。
附上AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 300 + 5;
int G[maxn][maxn];
int T, m, k, x, y;
int kase;
int judge()
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < m; j++)
{
int flag1 = 0, flag2 = 0;
for (int k = 0; k < m; k++)
{
if (G[i][k] && G[j][k])
flag1 = 1;
if (G[i][k] != G[j][k])
flag2 = 1;
}
if (flag1 && flag2)
return 0;
}
}
return 1;
}
int main()
{
ios::sync_with_stdio(false);
cin >> T;
while (T--)
{
memset(G, 0, sizeof(G));
cin >> m >> k;
for (int i = 0; i < k; i++)
{
cin >> x >> y;
G[x][y] = 1;
}
printf("Case #%d: ", ++kase);
if (judge())
printf("Yes\n");
else printf("No\n");
}
// system("pause");
return 0;
}