【DFS】 HDU 3298 Contestants
生活随笔
收集整理的這篇文章主要介紹了
【DFS】 HDU 3298 Contestants
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
隨便以一個點為根 計算其子樹和自己本身的點值
然后枚舉已切每個點的ans取最小值
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <string> #include <iostream> #include <algorithm> #include <sstream> #include <cmath> using namespace std; #include <queue> #include <stack> #include <vector> #include <deque> #include <set> #include <map> #include <time.h>; #define cler(arr, val) memset(arr, val, sizeof(arr)) #define IN freopen ("in.txt" , "r" , stdin); #define OUT freopen ("out.txt" , "w" , stdout); typedef long long LL; const int MAXN = 200010;//點數的最大值 const int MAXM = 200006;//邊數的最大值 const int INF = 0x3f3f3f3f; const int mod = 10000007; LL point[MAXN],num[MAXN]; vector<int>edge[MAXN]; bool vis[MAXN]; LL abs1(LL a) {if(a>0) return a;else return -a; } void dfs(int u) {vis[u]=true;for(int i=0;i<edge[u].size();i++){int v=edge[u][i];if(!vis[v]){dfs(v);point[u]+=point[v];}} } int main() {int m,n,cas=0;// IN;while(scanf("%d%d",&n,&m),m+n){LL sum=0;cler(point,0);cler(num,0);for(int i=1;i<=n;i++){edge[i].clear();scanf("%I64d",&point[i]);sum+=point[i];}for(int i=1;i<=m;i++){int a,b;scanf("%d%d",&a,&b);edge[a].push_back(b);edge[b].push_back(a);}LL ans=9999999999999;cler(vis,false);dfs(1);for(int i=1;i<=n;i++)ans=min(ans,abs1(sum-2*point[i]));printf("Case %d: %I64d\n",++cas,ans);} }轉載于:https://www.cnblogs.com/kewowlo/p/4002468.html
總結
以上是生活随笔為你收集整理的【DFS】 HDU 3298 Contestants的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Jmail的邮件发送
- 下一篇: JAVA句柄解释