HDU 2682--Tree【最小生成树】
来源:互联网 发布:淘宝卖家神笔入口 编辑:程序博客网 时间:2024/06/05 16:02
Play a game
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1673 Accepted Submission(s): 1349
Problem Description
New Year is Coming!
ailyanlu is very happy today! and he is playing a chessboard game with 8600.
The size of the chessboard is n*n. A stone is placed in a corner square. They play alternatively with 8600 having the first move. Each time, player is allowed to move the stone to an unvisited neighbor square horizontally or vertically. The one who can't make a move will lose the game. If both play perfectly, who will win the game?
ailyanlu is very happy today! and he is playing a chessboard game with 8600.
The size of the chessboard is n*n. A stone is placed in a corner square. They play alternatively with 8600 having the first move. Each time, player is allowed to move the stone to an unvisited neighbor square horizontally or vertically. The one who can't make a move will lose the game. If both play perfectly, who will win the game?
Input
The input is a sequence of positive integers each in a separate line.
The integers are between 1 and 10000, inclusive,(means 1 <= n <= 10000) indicating the size of the chessboard. The end of the input is indicated by a zero.
The integers are between 1 and 10000, inclusive,(means 1 <= n <= 10000) indicating the size of the chessboard. The end of the input is indicated by a zero.
Output
Output the winner ("8600" or "ailyanlu") for each input line except the last zero.
No other characters should be inserted in the output.
No other characters should be inserted in the output.
Sample Input
20
Sample Output
8600
#include <cstdio>#include <iostream>#include <algorithm>#include <cmath>using namespace std;#define maxn 1000100int per[maxn];int a[maxn]={1,1,0};int s[maxn];void prime(){ int i,j; for(i=2;i*i<maxn;++i) if(!a[i]){ for(j=i*2;j<maxn;j=j+i) a[j]=1; } return ;}struct node { int st,ed,u;};node str[maxn];int cmp(node a,node b){ return a.u<b.u;}int find(int x){ if(x==per[x]) return per[x]; return per[x]=find(per[x]);}int main (){ int t; prime(); scanf("%d",&t); while(t--){ int n,i,j; scanf("%d",&n); for(i=1;i<=n;++i){ per[i]=i; scanf("%d",&s[i]); } int k=0; for(i=1;i<=n;++i) for(j=i+1;j<=n;++j){ if(!a[s[i]] || !a[s[j]] || !a[s[i]+s[j]]){ str[k].st=i; str[k].ed=j; str[k].u=min(min(s[j],s[i]),abs(s[i]-s[j])); k++; } } sort(str,str+k,cmp); int ans=0,flag=0; long long sum=0; for(i=0;i<k;++i){ int fa=find(str[i].st); int fb=find(str[i].ed); if(fa!=fb){ per[fa]=fb; sum+=str[i].u; } } for(i=1;i<=n;++i){ if(per[i]==i) ans++; } if(ans>1) printf("-1\n"); else printf("%I64d\n",sum); } return 0;}
0 0
- hdu 2682 Tree 最小生成树
- HDU 2682--Tree【最小生成树】
- hdu 2682——Tree(最小生成树)
- hdu 2682 Tree 最小生成树 (并查集)
- HDU 2682 Tree 最小生成树 prim模板
- hdu 4786 Fibonacci Tree ( 最小生成树 )
- HDU 4786 Fibonacci Tree 最小生成树
- Tree 【最小生成树】
- hdu 2682 最小生成树
- hdu Minimal Ratio Tree(最小生成树---prim)
- hdu 2489 Minimal Ratio Tree(枚举+最小生成树)
- hdu 2489 Minimal Ratio Tree(dfs枚举 + 最小生成树)~~~
- HDU 4263 Red/Blue Spanning Tree(最小生成树)
- HDU 2489 Minimal Ratio Tree(数据结构-最小生成树)
- HDU 2489 Minimal Ratio Tree(图论-最小生成树)
- HDU 4786 Fibonacci Tree 最小生成树 kruskal算法
- HDU - 4786 Fibonacci Tree (最小生成树)
- HDU-#4786 Fibonacci Tree(最大最小生成树)
- 杀掉指定进程
- 猫猫学IOS(十一)UI之图片自动轮播
- Linux那些事儿 之 戏说USB(25)设备的生命线(八)
- linux命令-less
- java.util.concurrent源码学习系列--Executor
- HDU 2682--Tree【最小生成树】
- 自定义组合控件的过程
- Android手机修改host文件--顽固的小米3手机修改Host文件方法
- 珍惜时间
- 分析MP4的名人博客
- Ruby on Rails 成长记(2)
- Mac 下Versions的 svn无法上传 .a 文件的问题
- MySQL中的通配符[]
- Android Please ensure that adb is correctly located at有关问题解决