BestCoder Round #53 (div.2)(hdu5422,hdu5423,hdu5424)
来源:互联网 发布:济南淘宝代运营公司 编辑:程序博客网 时间:2024/06/05 02:29
1.Rikka with Graph
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5422
解题思路:
如果连上1-n的边,最短距离就是1。所以所有情况下最短距离都是1。
考虑方案数,如果本来没有1-n的边,那么只能连1-n,方案数为1。否则怎么连都可以,方案数是2n(n−1)。
AC代码:
#include <iostream>#include <cstdio>using namespace std;int main(){ int n,m; while(~scanf("%d%d",&n,&m)){ int u,v; int flag = 0; for(int i = 0; i < m; i++){ scanf("%d%d",&u,&v); if(u == 1 && v == n || u == n && v == 1) flag = 1; } if(flag) printf("1 %d\n",n*(n-1)/2); else printf("1 1\n"); } return 0;}
2.Rikka with Tree
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5423
解题思路:
http://blog.csdn.net/piaocoder/article/details/48129539
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> using namespace std; const int N = 1005; vector<int> v[N]; int sum[N]; int cnt;//深度 void dfs(int u,int fa,int dep){ int l = v[u].size(); for(int i = 0; i < l; i++){ int x = v[u][i]; if(x == fa) continue; dfs(x,u,dep+1); } sum[dep]++; cnt = max(cnt,dep); } bool check(){ for(int i = 1; i < cnt; i++) if(sum[i] > 1) return false; return true; } int main(){ int n; while(~scanf("%d",&n)){ int x,y; for(int i = 1; i<=n; i++) v[i].clear(); for (int i = 1; i<n; i++){ scanf("%d%d",&x,&y); v[x].push_back(y); v[y].push_back(x); } memset(sum,0,sizeof(sum)); cnt = 0; dfs(1, 1, 1); if(check()) printf("YES\n"); else printf("NO\n"); } return 0; }
3.Rikka with Graph II
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5424
解题思路:
http://blog.csdn.net/piaocoder/article/details/48129721
AC代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int n,cnt;int deg[1005],Map[1005][1005];bool flag,vis[1005];void dfs(int u){ if(cnt == n){ flag = 1; return; } for(int i = 1; i <=n && !flag; i++) if(!vis[i] && Map[u][i]) { vis[i] = 1; cnt++; dfs(i); cnt--; vis[i] = 0; }}int main(){ while(~scanf("%d",&n)){ flag=0; cnt=1; memset(deg,0,sizeof(deg)); memset(vis,0,sizeof(vis)); memset(Map,0,sizeof(Map)); int x,y; for(int i = 0; i < n; i++){ scanf("%d%d",&x,&y); if(x != y && !Map[x][y]){ Map[x][y]=Map[y][x]=1; deg[x]++; deg[y]++; } } int s = 1,tot = 0; for(int i = 1; i <= n; i++) if(deg[i] == 1){ s = i; tot++; } if(tot > 2){ printf("NO\n"); continue; } vis[s] = 1; dfs(s); if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- BestCoder Round #53 (div.2)(hdu5422,hdu5423,hdu5424)
- BestCoder Round #11 (Div. 2)
- BestCoder Round #11 (Div. 2)
- BestCoder Round #50 (div.2)
- BestCoder Round #50 (div.2)
- BestCoder Round 50 (div 2)
- BestCoder Round #50 (div.2)
- #BestCoder Round #50 (div.2)
- BestCoder Round #50 (div.2)
- BestCoder Round #54 (div.2)
- BestCoder Round #56 (div.2)
- BestCoder Round #58 (div.2)
- BestCoder Round #59 (div.2)
- BestCoder Round #59 (div.2)
- #BestCoder Round #59 (div.2)
- BestCoder Round #59 (div.2)
- BestCoder Round #61 (div.2)
- BestCoder Round #61 (div.2)
- MySQL单表百万数据记录分页性能优化
- java 克隆的浅拷贝与深拷贝
- Asp.net范例:amCharts(.net版)实现图形报表
- 【已解决】:让IE6 IE7 IE8 IE9 IE10 IE11支持Bootstrap
- Discuz门户keywords和description不能正确显示
- BestCoder Round #53 (div.2)(hdu5422,hdu5423,hdu5424)
- 关于C++回调函数设计的思考
- 编写高质量Objective-C代码
- 关于json返回日期格式化的解决方案
- Eclipse Mars Release (4.5.0)下安装编译Spark2.7.1
- [Bluetooth Core V4.2] VOL2, PartB, 6 Packets
- oh-my-zsh and auto-fu.zsh
- Android开源项目分类汇总
- iOS使用AVCaptureSession自定义相机