Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴一组数据,过了再交啊,否则WA1 3 4 2 3 2 3 2 3 1 3 ans 2 3 2 1或者2 3 3 1 附上AC java 代码 import java.io.*; public class Acm1719 { public static boolean [][]edge; public static int row; public static int column; public static boolean[]visit; public static int []path; /** * @param args */ public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub //java 快速输入,可以节省很多时间,类似于C语言的输入 StreamTokenizer input=new StreamTokenizer (new BufferedReader(new InputStreamReader(System.in))); input.nextToken(); int text=(int)input.nval; while(text--!=0){ input.nextToken(); row=(int)input.nval; input.nextToken(); column=(int)input.nval; edge=new boolean[row+1][column+1]; for(int i=1;i<column+1;i++){ input.nextToken(); int a=(int)input.nval; input.nextToken(); int b=(int)input.nval; edge[a][i]=true; edge[b][i]=true; } int ans=0; path=new int[column+1]; for(int i=1;i<row+1;i++){ visit=new boolean[column+1]; if(dfs(i))ans++; } //若最大匹配等于行数,则每行说明有一个被击中 if(ans==row){ for(int i=1;i<column+1;i++){ if(path[i]==0){ for(int j=1;j<row+1;j++){ //题目要求没列都要有一个输出,所以如果列大于行的时候,有些列没有匹配到, //这时随意输出该列白色格子的行数即可 if(edge[j][i]){ System.out.print(j+" "); break; } } } else System.out.print(path[i]+" "); } System.out.println(); } else System.out.println("NO"); } } //匈牙利算法,求最大匹配 public static boolean dfs(int a){ for(int i=1;i<column+1;i++){ if(!visit[i]&&edge[a][i]){ visit[i]=true; if(path[i]==0||dfs(path[i])){ path[i]=a; return true; } } } return false; } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator