首页 > 其他分享 >交换学生(Foreign Exchange)

交换学生(Foreign Exchange)

时间:2022-11-28 18:34:05浏览次数:37  
标签:Exchange int 交换 System Foreign 数组 input es out


Problem E
Foreign Exchange
Input:
standard input
Output: standard output
Time Limit: 1 second

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.

The program your organization runs works as follows: All candidates are asked for their original location and the location they would like to go to. The program works out only if every student has a suitable exchange partner. In other words, if a student wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to 500000 candidates!

Input

The input file contains multiple cases. Each test case will consist of a line containing n - the number of candidates (1≤n≤500000), followed by n lines representing the exchange information for each candidate. Each of these lines will contain 2 integers, separated by a single space, representing the candidate's original location and the candidate's target location respectively. Locations will be represented by nonnegative integer numbers. You may assume that no candidate will have his or her original location being the same as his or her target location as this would fall into the domestic exchange program. The input is terminated by a case where n = 0; this case should not be processed.

 

Output

For each test case, print "YES" on a single line if there is a way for the exchange program to work out, otherwise print "NO".

 

Sample Input                               Output for Sample Input

10

1 2

2 1

3 4

4 3

100 200

200 100

57 2

2 57

1 2

2 1

10

1 2

3 4

5 6

7 8

9 10

11 12

13 14

15 16

17 18

19 20

0

YES

NO

【分析】

       首先我所能想到的是(可惜该方法时间超限),先把所有的学生的A和B(出发地和目的地)都储存在二维数组中(第一列存A,第二列存B),然后在创建另个用于标记的数组,该数组标记当前行是否被访问过,假如已被访问说明该学生已经匹配到“对象”,就不能再帮他寻找“对象“了。从二维数组中的第一行开始进行匹配……

用java语言编写程序,代码如下:


import java.util.Scanner;
//Time limit exceeded
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

while(input.hasNext()) {
int n = input.nextInt();
if(n == 0)
break;


int[][] es = new int[n][2];
boolean[] isVisited = new boolean[n];//默认值为false

for(int i = 0; i < n; i++) {
es[i][0] = input.nextInt();
es[i][1] = input.nextInt();
}

if(n % 2 != 0) {
System.out.println("NO");
continue;
}

boolean result = true;
for(int i = 0; i < n - 2; i++) {
int j;
if(isVisited[i])
continue;
for(j = i + 1; j < n; j++) {
if(!isVisited[j] && es[i][0] == es[j][1] && es[i][1] == es[j][0]) {
isVisited[j] = true;
break;
}
}
if(j == n) {
result = false;
break;
}
//isVisited[i] = true;
}

if(result)
System.out.println("YES");
else
System.out.println("NO");
}
}
}


       另外一种思路是这样的,在读取每个学生的A和B的过程中,将A分在一个数组中、将B分在一个数组中(数组初始化为0),这个两个数组分别以A、B为下标进行自增,每次读取到A或B下标就对相应的数组的A或B下标的元素值加1。我们只要判断这两个数组对应下标的元素值是否相等即可。

用java语言编写程序,代码如下:


import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] f = new int[500010];
int[] t = new int[500010];

while(input.hasNext()) {
int n = input.nextInt();

if(n == 0)
break;

Arrays.fill(f, 0);
Arrays.fill(t, 0);

int max = 0;
for(int i = 0; i < n; i++) {
int A = input.nextInt();
int B = input.nextInt();
if(max < A) max = A;
if(max < B) max = B;
f[A]++;
t[B]++;
}

boolean result = true;
for(int i = 0; i <= max; i++) {
if(f[i] != t[i]) {
result = false;
break;
}
}
if(result)
System.out.println("YES");
else
System.out.println("NO");
}
}
}






标签:Exchange,int,交换,System,Foreign,数组,input,es,out
From: https://blog.51cto.com/u_15894233/5893369

相关文章

  • 关于交换机的VLAN技术你了解多少?
     VLAN(虚拟局域网)是对连接到的第二层交换机端口的网络用户的逻辑分段,不受网络用户的物理位置限制而根据用户需求进行网络分段。一个VLAN可以在一个交换机或者跨交换机实现。......
  • 交换机的RJ45端口和SFP端口有什么区别?
    现如今,随着人们对网络需求的日益增长,数据中心或者服务器机房内的网络升级已经刻不容缓,因此,千兆以太网已经越来越普遍。众所周知,目前市场上大家使用的千兆以太网交换机一般有......
  • PoE交换机选择时需要注意什么?
    如果你打算选择或者使用PoE交换机,这些知识点一定要看,可以让你少走弯路、少些麻烦。今天,飞畅科技的小编来为大家详细介绍下PoE交换机的选择和使用要点,一起来看看吧!一,PoE交换......
  • PoE交换机能代替普通工业交换机吗?
    日前PoE交换机需求量大,那一定有人疑问PoE交换机可以代替普通工业交换机使用吗?接下来由工业交换机厂家来为大家详细介绍下,一起来看看吧。一般情况下是可以的,具有IEEE802.3af......
  • PoE交换机为什么值得你选择?
    PoE交换机的应用范围很广泛,它与普通的交换机有什么区别呢?PoE交换机为什么值得你选择呢?接下来飞畅科技的小编就来为大家详细介绍下,一起来看看吧!PoE交换机除了具备普通交换机......
  • H3C交换机恢复出厂配置
    方法一<H3C> resetsaved-configurationmain //清空交换机配置Thesavedconfigurationfilewillbeerased.Areyousure?[Y/N]:y//设备提示保存的配置将会......
  • 实验2:Open vSwitch虚拟交换机实践
    实验2:OpenvSwitch虚拟交换机实践一、实验目的能够对OpenvSwitch进行基本操作;能够通过命令行终端使用OVS命令操作OpenvSwitch交换机,管理流表;能够通过Mininet的......
  • SDN-实验2:Open vSwitch虚拟交换机实践
    1.ovs-vsctl基础操作实践:创建OVS交换机,以ovs-xxxxxxxxx命名,其中xxxxxxxxx为本人学号。在创建的交换机上增加端口p0和p1,设置p0的端口号为100,p1的端口号为101,类型均为interna......
  • 中小型企业华为路由器+防火墙+核心交换机网络部署
    网络组网图:  网络规划:办公网VLAN:10,IP地址段:192.168.10.0/24,网关192.168.10.254生产网络VLAN:20,IP地址段:192.168.20.0/24,网关192.168.20.254生产服务器地址段:172.16......
  • leetcode 24. 两两交换链表中的节点 js实现
    给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。示例1:输入:head=[1,2,3,4]输出:[2,......