UVA - 11175 From D to E and Back

点击打开题目链接

From D to E and Back

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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值