usaco 5.3.5 Network of Schools
2014-10-05 20:37
316 查看
/* ID: daniel.20 LANG: JAVA TASK: schlnet */ import java.util.*; import java.io.*; class schl{ int g[][] = new int[101][101]; int n; int color[] = new int[101]; int parent[] = new int[101]; int sons[] = new int[101]; int g2[][] = new int[101][101]; void solver() throws IOException{ BufferedReader reader = new BufferedReader(new FileReader("schlnet.in")); //BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("schlnet.out"))); n = Integer.valueOf(reader.readLine()); for(int i=0;i<n;i++){ StringTokenizer st = new StringTokenizer(reader.readLine()); while(st.hasMoreTokens()){ int tmp = Integer.valueOf(st.nextToken()); if(tmp!=0) { g[i+1][tmp] = 1; g2[i+1][tmp]=1; g2[tmp][i+1]=1; sons[i+1]++; parent[tmp]++; } } } int c = 1; for(int i=1;i<=n;i++){ if(color[i]==0&&parent[i]==0){ dfs(i,c); c++; } } for(int i=1;i<=n;i++){ if(color[i]==0){ dfs2(i,c); c++; } } c--; pw.println(c); System.out.println(c); int count=0;int tmp2=0; for(int i=1;i<=n;i++){ if(sons[i]==0) tmp2++; } count=c==1?0:c; if(tmp2>count)count=tmp2; pw.println(count); System.out.println(count); pw.close(); } void dfs(int a, int b){ color[a] = b; for(int i=1;i<101;i++){ if(g[a][i]==1&&color[i]==0){ dfs(i,b); } } } void dfs2(int a, int b){ color[a] = b; for(int i=1;i<101;i++){ if(g2[a][i]==1&&color[i]==0){ dfs(i,b); } } } } public class schlnet { public static void main(String[] args) throws Exception { schl p = new schl(); p.solver(); } }
第一问,入度为零的点加块的数量
第二问,出度为零点和第一问结果取大
当整个图全联通时,第一问取零
相关文章推荐
- USACO 5.3 Network of Schools(Tarjan)
- [IOI1996] USACO Section 5.3 Network of Schools(强连通分量)
- usaco Network of Schools
- USACO Section 5.3 Network of Schools - 回顾Tarjan求强连通分量
- 洛谷 2746 POJ 1236 SSL 1920 [USACO5.3] 校园网 Network of Schools
- P2746 [USACO5.3]校园网Network of Schools
- [USACO5.3]校园网Network of Schools
- 洛谷P2746 [USACO5.3]校园网Network of Schools
- [USACO5.3]校园网Network of Schools 缩点
- 洛谷 2746 poj 1236 [USACO5.3] 校园网 Network of Schools (邻接表)
- usaco5.3.3 Network of Schools
- usaco 5.3 Network of Schools(强连通分量+构造强连通最少边)
- 洛谷P2746 [USACO5.3]校园网Network of Schools
- usaco training 5.3.3 Network of Schools 题解
- 【USACO5.3.3】Network of Schools
- [USACO5.3]校园网Network of Schools 强连通分量
- usaco Network of Schools
- USACO5.3.3 Network of Schools (schlnet)
- USACO Network of Schools 解题报告
- Network of Schools[USACO]