• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

2013 Asia Chengdu Regional Contest F

Fibonacci Tree

Time Limit: 4000/2000 MS (Java/Others)    

Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 266    Accepted Submission(s): 82

Problem Description
  Coach Pang is interested in Fibonacci numbers while Uncle Yang wants him to do some research on Spanning Tree. So Coach Pang decides to solve the following problem:   Consider a bidirectional graph G with N vertices and M edges. All edges are painted into either white or black. Can we find a Spanning Tree with some positive Fibonacci number of white edges? (Fibonacci number is defined as 1, 2, 3, 5, 8, ... )
 
Input
  The first line of the input contains an integer T, the number of test cases.   For each test case, the first line contains two integers N(1 <= N <= 105) and M(0 <= M <= 105).   Then M lines follow, each contains three integers u, v (1 <= u,v <= N, u<> v) and c (0 <= c <= 1), indicating an edge between u and v with a color c (1 for white and 0 for black).
 
Output
  For each test case, output a line “Case #x: s”. x is the case number and s is either “Yes” or “No” (without quotes) representing the answer to the problem.
 
Sample Input
2
4 4
1 2 1
2 3 1
3 4 1
1 4 0
5 6
1 2 1
1 3 1
1 4 1
1 5 1
3 5 1
4 2 1
 
Sample Output
Case #1: Yes
Case #2: No
 
Source
2013 Asia Chengdu Regional Contest
 
Recommend
We have carefully selected several similar problems for you:  4789 4788 4787 4785 4784 
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <vector>
  4 #include <set>
  5 #include <cstring>
  6 #include <string>
  7 #include <map>
  8 #include <cmath>
  9 #include <ctime>
 10 #include <algorithm>
 11 #include <queue>
 12 
 13 using namespace std;
 14 #define INF 0x7fffffff
 15 #define maxm 1001
 16 #define mp make_pair
 17 #define pb push_back
 18 #define rep(i,n) for(int i = 0; i < (n); i++)
 19 #define re return
 20 #define fi first
 21 #define se second
 22 #define sz(x) ((int) (x).size())
 23 #define all(x) (x).begin(), (x).end()
 24 #define sqr(x) ((x) * (x))
 25 #define sqrt(x) sqrt(abs(x))
 26 #define y0 y3487465
 27 #define y1 y8687969
 28 #define fill(x,y) memset(x,y,sizeof(x))
 29 
 30 typedef vector<int> vi;
 31 typedef long long ll;
 32 typedef long double ld;
 33 typedef double D;
 34 typedef pair<int, int> ii;
 35 typedef vector<ii> vii;
 36 typedef vector<string> vs;
 37 typedef vector<vi> vvi;
 38 
 39 template<class T> T abs(T x) { re x > 0 ? x : -x; }
 40 
 41 const int maxn = 200005;
 42 
 43 int n, m, t, s, k;
 44 int vis[maxn];
 45 //string s;
 46 struct Node{
 47     int u, dist;
 48     Node(int a, int b){ u = a, dist=b; }
 49     bool operator<(const Node& cmp)const{
 50         return dist>cmp.dist;
 51     }
 52 };
 53 vector<int>A;
 54 struct edge{
 55     int from, to, dist;
 56     edge(int a,int b,int c){
 57         from = a, to = b, dist = c;
 58     }
 59 };
 60 vector<edge>edges;
 61 vector<int>g[maxn];
 62 void add(int from, int to, int dist){
 63     edges.push_back(edge(from, to, dist));
 64     int sz = edges.size();
 65     g[from].push_back(sz - 1);
 66 }
 67 void init(){
 68     for (int i = 0; i < maxn; i++)g[i].clear();
 69     edges.clear();
 70 }
 71 int Prim1(int from){
 72     priority_queue<Node>q;
 73     while (!q.empty())q.pop();
 74     int dist = 0, t = 0;
 75     q.push(Node(from,0));
 76     while (!q.empty()&&t< n){
 77         Node x = q.top(); q.pop();
 78         if (vis[x.u])continue;
 79         vis[x.u] = 1;
 80         dist += x.dist;
 81         t++;
 82         for (int i = 0; i < g[x.u].size(); i++){
 83             edge e = edges[g[x.u][i]];
 84             if (!vis[e.to]){ q.push(Node(e.to, e.dist)); }
 85         }
 86     }
 87     if (t < n)return -1;
 88     return dist;
 89 }
 90 int f[maxn];
 91 int fr;
 92 int main(){
 93     f[0] = 1; f[1] = 2;
 94     for (int i = 2;i<=40; i++)f[i] = f[i - 1] + f[i - 2];
 95     int T,cas=1;
 96     scanf("%d", &T);
 97     while(T--){
 98         init();
 99         scanf("%d%d", &n, &m);
100         for (int i = 0; i < m; i++){
101             int a, b, c;
102             scanf("%d%d%d", &a, &b, &c);
103             add(a, b, c);
104             add(b, a, c);
105             fr = a;
106         }
107         memset(vis, 0, sizeof vis);
108         int s=Prim1(fr);
109         memset(vis, 0, sizeof vis);
110         for (int i = 0; i < edges.size(); i++)edges[i].dist = 1 - edges[i].dist;
111         int t = Prim1(fr);
112         printf("Case #%d: ", cas++);
113         if (t == -1){ printf("No\n"); continue; }
114         int t = n - 1 - t;
115         int flag = 0;
116         for (int i = 0; i<=39; i++)if (s<=f[i]&&f[i]<=t)flag = 1;
117         if (flag)printf("Yes\n");
118         else printf("No\n");
119     }
120     return 0;
121 }
View Code
posted @ 2013-11-21 14:08  HaibaraAi  阅读(160)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3