HDU——1054 Strategic Game
生活随笔
收集整理的這篇文章主要介紹了
HDU——1054 Strategic Game
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Strategic Game
Time Limit: 20000/10000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8504????Accepted Submission(s): 4094
Your program should find the minimum number of soldiers that Bob has to put for a given tree.
The input file contains several data sets in text format. Each data set represents a tree with the following description:
the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)
The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.
For example for the tree:?
?
the solution is one soldier ( at the node 1).
The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table: Sample Input 4 0:(1) 1 1:(2) 2 3 2:(0) 3:(0) 5 3:(3) 1 4 2 1:(1) 0 2:(0) 0:(0) 4:(0) Sample Output 1 2 Source Southeastern Europe 2000 Recommend JGShining???|???We have carefully selected several similar problems for you:??1053?1151?1142?1233?1045?
問題描述
鮑勃喜歡玩電腦游戲,特別是戰略游戲,但有時候他無法快速找到解決方案,那么他很傷心。現在他有以下問題。他必須捍衛一座中世紀城市,其道路形成一棵樹。他必須將最少數量的士兵放在節點上,以便他們可以觀察所有的邊緣。你的程序應該找到Bob給給定樹的最小兵數。
思路:用最少的節點覆蓋所有的邊。最小點覆蓋裸題=最大匹配數
這個題的數據比較大,用鄰接表的話會T,用鄰街鏈表的話可以A
代碼:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define N 1500+10 using namespace std; bool vis[N]; int n,m,k,x,y,tot,head[N],girl[N],map[N][N]; int read() {int x=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();}return x*f; } struct Edge {int from,to,next; }edge[N]; int add(int x,int y) {tot++;edge[tot].to=y;edge[tot].next=head[x];head[x]=tot; } int find(int x) {for(int i=head[x];i;i=edge[i].next){int t=edge[i].to;if(!vis[t]){vis[t]=true;if(girl[t]==-1||find(girl[t])) {girl[t]=x;return 1;}}}return 0; } int main() {int t=0,sum,ans;while(scanf("%d",&n)!=EOF){ans=0,sum=0;tot=0;memset(map,0,sizeof(map));memset(head,0,sizeof(head));for(int i=1;i<=n;i++){x=read();m=read();while(m--){y=read();add(x+1,y+1),add(y+1,x+1);}} memset(girl,-1,sizeof(girl));for(int i=1;i<=n;i++){memset(vis,0,sizeof(vis));if(find(i)) ans++;}printf("%d\n",ans/2);}return 0; }?
轉載于:https://www.cnblogs.com/z360/p/7435613.html
總結
以上是生活随笔為你收集整理的HDU——1054 Strategic Game的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mstsc.exe是什么(重启的命令是什
- 下一篇: imm32.dll是什么