hdu 4786 Fibonacci Tree (最小生成樹)
題意:一共n個點m條邊,每條邊有個權值1代表白邊,0代表黑邊,求能否建一個生成樹,使得白邊的數量是一個斐波那契數列
思路:求得是白邊的數,可以求出白邊的上限與下限,再在這個區間內找有沒有屬於斐波拉契數列中的數,求上下限時用kruskal演算法,優先選用白邊,求出白邊數的上限,優先選用黑邊,求出白邊數的下限。
#include <bits/stdc++.h>
#define eps 1e-8
#define INF 0x3f3f3f3f
#define PI acos(-1)
#define lson l,mid,rt<<1
#define rson mid+1,r,(rt<<1)+1
#define CLR(x,y) memset((x),y,sizeof(x))
#define fuck(x) cerr << #x << "=" << x << endl
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int seed = 131;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
int a[maxn];
struct node {
int u, v, w;
node() {}
node(int u, int v, int w): u(u), v(v), w(w) {}
} e[2 * maxn];
int tot;
bool cmp1(node a, node b) {
return a.w > b.w;
}
bool cmp2(node a, node b) {
return a.w < b.w;
}
int f[maxn];
int getf(int v) {
return f[v] == v ? v : f[v] = getf(f[v]);
}
int T, n, m;
int flag;
bool vis[maxn];
int main() {
a[1] = 1;
a[2] = 2;
for (int i = 3; i <= 100000; i++) {
if (a[i - 1] > 1e5) break;
a[i] = a[i - 1] + a[i - 2];
}
scanf("%d", &T);
for (int cas = 1; cas <= T; cas++) {
printf("Case #%d: ", cas);
tot = 0;
flag = 0;
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) f[i] = i;
for (int i = 1; i <= m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
e[tot++] = {u, v, w};
}
sort(e, e + tot, cmp2);
int MIN = 0;
int cnt = 0;
for (int i = 0; i < tot; i++) {
if (cnt == n - 1) break;
int t1 = getf(e[i].u);
int t2 = getf(e[i].v);
if (t1 != t2) {
f[t2] = t1;
cnt++;
MIN += e[i].w;
}
}
if (cnt != n - 1) {
printf("No\n");
continue;
}
for (int i = 1; i <= n; i++) f[i] = i;
int MAX = 0;
sort(e, e + tot, cmp1);
cnt = 0;
for (int i = 0; i < tot; i++) {
int t1 = getf(e[i].u);
int t2 = getf(e[i].v);
if (cnt == n - 1) break;
if (t1 != t2) {
f[t2] = t1;
cnt++;
MAX += e[i].w;
}
}
for (int i = 1; i <= 100009; i++) {
if (a[i] > MAX) break;
if (a[i] <= MAX && a[i] >= MIN) flag = 1;
}
if (flag) printf("Yes\n");
else printf("No\n");
}
return 0;
}
相關推薦
hdu 4786 Fibonacci Tree (最小生成樹)
題意:一共n個點m條邊,每條邊有個權值1代表白邊,0代表黑邊,求能否建一個生成樹,使得白邊的數量是一個斐波那契數列 思路:求得是白邊的數,可以求出白邊的上限與下限,再在這個區間內找有沒有屬於斐波拉契數列中的數,求上下限時用kruskal演算法,優先選用白邊,求
HDU-2489 Minimal Ratio Tree(最小生成樹)
figure tro line accep tree one 分享 origin regional Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768
HDU 3371(城市聯通 最小生成樹-Kruskal)
fat names none one ios div style += 技術 題意是求將所有點聯通所花費的最小金額,如不能完全聯通,輸出 -1 直接Kruskal,本題帶來的一點教訓是 rank 是algorithm頭文件裏的,直接做變量名會導致編譯錯誤。沒查到 rank
HDU 4463 Outlets 【最小生成樹】
let .net 鏈接 全部 題目 () 生成樹 father for <題目鏈接> 題目大意: 給你一些點的坐標,要求你將這些點全部連起來,但是必須要包含某一條特殊的邊,問你連起這些點的總最短距離是多少。 解題分析: 因為一定要包含那條邊,我們就記錄下那條邊的
HDU-Jungle Roads(最小生成樹)
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads be
HDU 1863 暢通工程(最小生成樹模板題)
kruskal,prim隨便搞 #include<cstdio> #include<algorithm> const int L=100005; struct node{int s,y,w;}edge[L]; int Fa[L],n,m; void
HDU 4786(最小生成樹 kruskal)
desc cpp using tran soft fine put sea can 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4786 Problem Description Coach Pang is
HDU 2489 Minimal Ratio Tree (dfs+Prim最小生成樹)
tracking mode pid cas cond multi ima ces bold 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 Problem Description For a tree,
HDU2489 Minimal Ratio Tree 【DFS】+【最小生成樹Prim】
mini note 推斷 sym hash %d ted n) lin Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot
【BZOJ2654】tree 二分+最小生成樹
con 連通圖 顏色 kruskal bool cpp || esp 答案 【BZOJ2654】tree Description 給你一個無向帶權連通圖,每條邊是黑色或白色。讓你求一棵最小權的恰好有need條白色邊的生成樹。 題目保證有解。 Input
HDU 4081Qin Shi Huang's National Road System(最小生成樹+最小瓶頸路)
continue ++ ng- ims cstring 題意 cost 圖的最小生成樹 span 題意:秦始皇要修路。把N個城市用N-1條邊連通。且他希望花費最小,可是這時候有一個多管閑事的道士出來說他有魔法能夠幫助秦始皇變成
HDU 4081 Peach Blossom Spring (最小生成樹+dfs)
clear put max 最大 ios light define algo puts 題意:給定一個 n 個點和相應的權值,要求你用 n-1 條邊連接起來,其中一條邊是魔法邊,不用任何費用,其他的邊是長度,求該魔法邊的兩端的權值與其他邊費用的盡量大。 析:先求出最小生成樹
HDU 6187 Destroy Walls (對偶圖最小生成樹)
技術分享 -c pro esp bsp {} 一個人 個人 return 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6187 題意:有一個V個結點M條邊的帶邊權無向平面圖,有一個人在一個區域,要拆一些墻使得他可以到達任意一
hdu 1863 [【最小生成樹】+hdu2544【floyed】+hdu1874【dijdtra】~~~模板復習~~~
ref define str print break ++ 題目 n) div 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 #include<stdio.h> #include<strin
BZOJ2654 tree 【二分 + 最小生成樹】
ace spa line check esp 直接 sin inf log 題目 給你一個無向帶權連通圖,每條邊是黑色或白色。讓你求一棵最小權的恰好有need條白色邊的生成樹。 題目保證有解。 輸入格式 第一行V,E,need分別表示點數,邊數和需要的白色邊數。 接下來E行
HDU 1223 還是暢通過程【最小生成樹模板】
tro int urn click desc sizeof mission names 技術分享 還是暢通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe
BZOJ.1016.[JSOI2008]最小生成樹計數(Matrix Tree定理 Kruskal)
main mat 計算 def tdi str 題目 matrix include 題目鏈接 最小生成樹有兩個性質: 1.在不同的MST中某種權值的邊出現的次數是一定的。 2.在不同的MST中,連接完某種權值的邊後,形成的連通塊的狀態是一樣的。 \(Solution1\)
HDU 1162 Eddy's picture (最小生成樹 prim)
坐標 .cn for paper cati contain discover NPU first 題目鏈接 Problem Description Eddy begins to like painting pictures recently ,he is sure of h
BZOJ 2654: tree(二分 最小生成樹)
memory med har stat 連通 註意 題目 sub mit Time Limit: 30 Sec Memory Limit: 512 MBSubmit: 2901 Solved: 1196[Submit][Status][Discuss] Descrip
最小生成樹:Tree
namespace 註意點 memset 數據 最小生成樹 每次 替換 個性 順序 參考資料:https://blog.csdn.net/sunshinezff/article/details/48749453 題解摘抄: 顯然可以發現隨著白邊權值的增大。最小生成樹中白邊的