CCF之送货(java)
来源:互联网 发布:nba2k17库里捏脸数据 编辑:程序博客网 时间:2024/04/30 05:33
解题代码(java):
import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.Stack; import java.util.Vector; public class Main { public static Vector<Integer> []G = new Vector[10010]; public static int []d = new int[10010]; public static boolean [][]edge = new boolean [10010][10010]; public static Stack<Integer> S = new Stack<Integer>(); public static Queue<Integer> q = new LinkedList<Integer>(); static int bfs(int s,int n){ boolean []vis = new boolean[10010]; q.add(s); vis[s]=true; while(!q.isEmpty()){ int u=q.remove(); for(int i=0;i<G[u].size();++i){ int v=G[u].get(i); if(!vis[v]){ vis[v]=true; q.add(v); } } } int x = 0; for(int i = 1;i<n+1;i++) { if(!vis[i]) { x++; } } return x; } private static void DFS1(int u,int n) { for(int i=0;i<G[u].size();++i){ int v=G[u].get(i); if(!edge[u][v]){ edge[u][v]=edge[v][u] =true; DFS1(v,n); S.push(v); } } } static class comp implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { // TODO Auto-generated method stub return o1-o2; } } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); for(int i = 0;i<n+1;i++) { G[i] = new Vector<Integer>(); } for(int i = 1;i<m+1;i++) { int x = in.nextInt(); int y = in.nextInt(); G[x].add(y); G[y].add(x); d[x]++; d[y]++; } in.close(); int number = 0; for (int i=1;i<n+1;i++) { if (d[i]%2!=0) number++; } if (bfs(1,n)==0 && (number==0 ||number==2)) { for(int i = 1;i<n+1;i++) { Collections.sort(G[i]); } DFS1(1,n); System.out.print(1); while(!S.empty()) { System.out.print(" "+S.pop()); } } else System.out.println(-1); } }运行超时,算法没学好,希望大神帮忙,50分
0 0
- CCF之送货(java)
- ccf 送货
- ccf 送货
- ccf送货
- ccf 送货
- CCF 送货
- CCF送货
- CCF 送货
- CCF CSP 送货 JAVA 201512-4
- CCF认证 送货
- CCF 201512-4 送货
- ccf 201512-4 送货
- CCF送货(欧拉图)
- CCF 201512-4 送货
- 【CCF 送货 waing】
- 201512-4 送货 ccf
- ccf 201512-4送货
- CCF——送货(欧拉路径)
- javascript的重要知识
- 【软工文档】-总结
- 已知二叉树中序,前序(或后序 -求后序(或前序
- 面试题(一)
- Javascript中new Date的坑
- CCF之送货(java)
- Sqoop使用实例讲解
- 欢迎使用CSDN-markdown编辑器
- Spring-Boot添加日志和使用spring-boot-devtools(热部署)
- 1011. A+B和C (15)
- Java异常的深入研究与分析
- C/C++中的堆栈分配情况
- 基于AsyncTask的ImageLoader网络图片加载
- Android Studio 自定义皮肤主题和背景