PAT1013
2016-01-30 21:38
381 查看
PAT1013
1013. Battle Over Cities (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.
Output
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input3 2 3
1 2
1 3
1 2 3
Sample Output1
0
0
本题就是求一个图中的联通分量的个数,使用并查集算法解决
1013. Battle Over Cities (25)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.
Output
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input3 2 3
1 2
1 3
1 2 3
Sample Output1
0
0
本题就是求一个图中的联通分量的个数,使用并查集算法解决
import java.util.ArrayList; import java.util.Scanner; public class Main2 { static int N,M,K; static int[] gen; static int[] high; public static void main(String[] args) { Scanner scanner=new Scanner(System.in); N=scanner.nextInt(); M=scanner.nextInt(); K=scanner.nextInt(); gen=new int[N+1]; high=new int[N+1]; int cut; ArrayList<A> arrayList=new ArrayList<A>(); for(int i=0;i<M;i++) arrayList.add(new A(scanner.nextInt(),scanner.nextInt())); for(int i=0;i<K;i++){ cut=scanner.nextInt(); init(); for(int j=0;j<M;j++){ int m=arrayList.get(j).xx; int n=arrayList.get(j).yy; if(m==cut||n==cut) continue; if(gen[m]==gen ) continue; union(m, n); } int cnt=0; for(int j=1;j<=N;j++){ if(gen[j]==j && j!=cut) cnt++; } System.out.println(cnt-1); } } public static int getgen(int x){ if(x==gen[x]) return x; return gen[x]=getgen(gen[x]); } public static void union(int a,int b){ int x=getgen(a); int y=getgen(b); if(high[x]<=high[y]){ gen[x]=y; high[y]+=high[x]; } else{ gen[y]=x; high[x]+=high[y]; } } public static void init(){ for(int i=1;i<=N;i++){ gen[i]=i; high[i]=1; } } } class A{ public int xx; public int yy; A(int a,int b){ xx=a; yy=b; } }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树