题目:
从一堆钻石中随机抽取两颗并比较重量。这些钻石的重量各不相同。在他们们比较了一段时间后,它们看中了两颗钻石g1和g2。
给定两颗钻石的编号g1,g2,比较次数n,编号从1开始,
同时给定关系数组a[n][2],第一个元素为一次比较中较重的钻石的编号,第二个元素为较轻的钻石的编号。
请返回这两颗钻石的关系,若g1更重返回1,g2更重返回-1,无法判断返回0。
输入数据保证合法,不会有矛盾情况出现。
测试样例:
2 3 4
1 2
2 4
1 3
4 3
返回: 1
本题y与Floyd稍有不同,并不需要求出最短路径
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int g1=sc.nextInt();
int g2=sc.nextInt();
int n=sc.nextInt();
int records[][]=new int[n][2];
for(int i=0;i<n;i++){
records[i][0]=sc.nextInt();
records[i][1]=sc.nextInt();
}
sc.close();
for(int i = 0; i < n; i++) {
if(records[i][0] == g1 && records[i][1] == g2) {
System.out.println(1);
return;
}
if(records[i][0] == g2 && records[i][1] == g1) {
System.out.println(-1);
return;
}
if(records[i][0] == g1) {
int tmp = records[i][1];
for(int j = 0; j < n; j++) {
if(records[j][0] == tmp && records[j][1] == g2) {
System.out.println(1);
return;
}
}
}
if(records[i][0] == g2) {
int tmp = records[i][1];
for(int j = 0; j < n; j++) {
if(records[j][0] == tmp && records[j][1] == g1) {
System.out.println(-1);
return;
}
}
}
}
System.out.println(0);
}
}