PAT1013
来源:互联网 发布:java商品交易系统 编辑:程序博客网 时间:2024/06/05 06:01
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
本题就是求一个图中的联通分量的个数,使用并查集算法解决
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[n]) 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; }}
- pat1013
- PAT1013
- PAT1013
- PAT1013
- PAT1013. 数素数
- PAT1013. Battle Over Cities
- pat1013 Battle Over Cities
- PAT1013数素数
- pat1013:数素数
- pat1013. 数素数 (20)
- PAT1013. 数素数 (20)
- PAT1013数素数~~~
- PAT1013 数素数
- PAT1013 并查集
- PAT1013. Battle Over Cities (25)
- PAT1013 BASIC:数素数 (20)
- 从零单排PAT1013,1014
- PAT1013. Battle Over Cities (25)
- JVM内存管理机制
- MySQL的经典用法
- SVN服务器常见错误
- python学习之9 日志记录logging
- Linux学习之源码2:start_kernel流程
- PAT1013
- Dynamic templateUrl in directives
- LZW压缩与解压
- GCD的相关东东
- C# 滚动条应用总结
- C# richTextBox TextBox赋值失败的奇葩问题
- 1039. 到底买不买(20)
- flask+heroku环境搭建(windows)
- dataTables 添加行内操作按钮