当前位置:网站首页>有向图D和E
有向图D和E
2022-06-23 12:53:00 【chengqiuming】
一 原问题描述
From D to E and Back - UVA 11175 - Virtual Judge
https://vjudge.net/problem/UVA-11175
二 输入和输出
1 输入
第1行包括测试用例数N(N<220)。在每个测试用例的前两行都包含 m 和 k,表示图 E 中节点数和边数。下面的 k 行,每行都包含两个节点 x 和 y,表示在 E 中从 x 到 y 有一条边。节点编号从 0 ~ m-1。
2 输出
对每个测试用例,都输出一行 Case #t,t表示测试用例编号,然后是 Yes 或者 No,用于判断 E 是否是一个有向图 D 的 Lying 图。
3 输入样例
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
4 输出样例
Case #1: Yes
Case #2: Yes
Case #3: No
Case #4: Yes
三 问题分析
本问题实际上就是把 D 中的边缩成点,D 中的一条边对应 E 中的一个节点,如果在 D 中存在边i(u、v)和 j(v、w),则 E 将具有从节点 i 到节点 j 的边。


如果在 D 中边 i 和边 j 有公共端点,则 i 连接的边,j 一定也连接,不存在 i 连接的边但是 j 没连接的情况。那么在 E 中,节点 i 和节点 j 有公共邻接点,则 i 邻接的节点,j 一定也邻接。如下图所示,在 D 中,边 i 和边 j 有公共端点 c,i 连接边 k1 和 k2,j 则一定也连接边 k1、k2;在对应的 E 中,节点 i 和节点 j 有公共邻接点 k1,i 有邻接点 k2,j 则一定也有邻接点 k2。

四 算法设计
1 用邻接矩阵存储 E。
2 判断在 E 中是否存在节点 i 和节点 j 有公共邻接点,如果存在,再判断是否存在对 i 有邻接的节点但是对 j 没有邻接的节点的情况,如果存在,就说明该 E 图不是一个有向图 D 的 Lying 图。
五 代码实现
package graph;
import java.util.Scanner;
public class UVA11175 {
static final int maxn = 300 + 5;
static int n;
static int m;
static boolean solve(int g[][]) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
boolean flag1 = false, flag2 = false;
for (int k = 0; k < n; k++) {
if (g[i][k] == 1 && g[j][k] == 1) // i=0 j=2 k=1
flag1 = true;
if (g[i][k] != g[j][k])
flag2 = true;
}
if (flag1 && flag2)
return false;
}
}
return true;
}
public static void main(String[] args) {
int T, cnt = 0, x, y;
Scanner scanner = new Scanner(System.in);
int g[][] = new int[maxn][maxn];
n = scanner.nextInt();
m = scanner.nextInt();
for (int i = 0; i < m; i++) {
x = scanner.nextInt();
y = scanner.nextInt();
g[x][y] = 1;
}
if (solve(g))
System.out.println("Case #" + ++cnt + ": Yes");
else
System.out.println("Case #" + ++cnt + ": No");
}
}六 测试
绿色为输入,白色为输出

边栏推荐
- 同花顺网上开户安全吗,需要注意什么
- Stimulsoft Ultimate Reports 2022.3.1
- R语言dplyr包filter函数过滤dataframe数据中指定数据列的内容包含指定字符串的数据行、基于grepl函数
- C# 文件下载方式
- 逆向调试入门-了解PE结构文件
- When did the redo log under InnoDB in mysql start to perform check point disk dropping?
- 能把SAP系统玩成鸡肋的公司,太有才了!
- #云原生征文#深入了解Ingress
- 手机开户有什么风险吗?开户安全吗?
- Configure SSH Remote Login for H3C switch
猜你喜欢

Analyse et résolution des défaillances de connexion causées par MySQL utilisant replicationconnection

Ablebits Ultimate Suite for Excel

What are the criteria for judging the end of the test?

Go写文件的权限 WriteFile(filename, data, 0644)?

LM05丨曾经的VIX(二代产品)

Can cold plate, submerged and spray liquid cooling lead the development of high-performance computing?
![解决“Thread 1: “-[*.CollectionNormalCellView isSelected]: unrecognized selector sent to instance 0x7f”](/img/35/65511c49eca5ae8a1896d776b479d9.jpg)
解决“Thread 1: “-[*.CollectionNormalCellView isSelected]: unrecognized selector sent to instance 0x7f”

AAIG看全球6月刊(上)发布|AI人格真的觉醒了吗?NLP哪个细分方向最具社会价值?Get新观点新启发~

华三交换机配置SSH远程登录

Based on your work experience, talk about the quality system construction in software testing
随机推荐
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
Analyse et résolution des défaillances de connexion causées par MySQL utilisant replicationconnection
Principle analysis of three methods for exchanging two numbers
Have you ever encountered incompatibility between flink1.15.0 and Flink CDC MySQL 2.2.1? f
Playing in Singapore in the hot summer: an inventory of indoor attractions and good places for night trips
Online text entity extraction capability helps applications analyze massive text data
在线文本过滤小于指定长度工具
R语言使用MASS包的polr函数构建有序多分类logistic回归模型、使用exp函数和coef函数获取模型中每个变量(自变量改变一个单位)对应的优势比(odds ratio)
支持HomeKit、NFC:智汀智能门锁SL1仅需要149元
同花顺网上开户安全吗,需要注意什么
Go写文件的权限 WriteFile(filename, data, 0644)?
华三交换机配置SSH远程登录
Hanyuan hi tech 1-channel gigabit optical port to 4-channel Gigabit Ethernet electrical port Gigabit 1-optical 4-electric optical fiber transceiver
2-optical-2-electric cascaded optical fiber transceiver Gigabit 2-optical-2-electric optical fiber transceiver Mini embedded industrial mine intrinsic safety optical fiber transceiver
Online text filter less than specified length tool
When did the redo log under InnoDB in mysql start to perform check point disk dropping?
深入思考:《盖亚奥特曼》中部分情景深度分析及反射出的哲理与感悟
4E1 PDH optical transceiver 19 inch rack type single fiber transmission 20km E1 interface optical network optical transceiver
夏日炎炎玩转新加坡:盘点室内景点和夜游好去处
Stimulsoft Ultimate Reports 2022.3.1