zoukankan      html  css  js  c++  java
  • AOJ-795 西瓜理发记(三)

    Description
    顺利潜入勺林寺几天后,方丈给了西瓜一个光荣而艰巨的任务——打扫寺庙中的道路。同时给了西瓜一张勺林寺的地图。
    西瓜这才知道,勺林寺中总共有n座房子,但道路只有n-1条,这n-1条道路连接了寺中的所有房子,即保证在任何两座房子都能沿着道路抵达。
    好在西瓜人缘不错,他知道每座房子中都有个自己的朋友,只要给他们每个人打个电话,让他到自己这里来,顺便把路也扫了,即给某座房子中的朋友打过电话后,可认为从该房子到西瓜所在的位置之间所有的道路都会被打扫干净。
    同时西瓜还知道,这n-1条道路中有一些路昨天已经被人打扫过了不需要再打扫一遍。
    现在西瓜想知道,自己最少要给多少个朋友打电话才能完成方丈给的任务。
    西瓜在编号为1的房子中。

    Input
    输入包含多组数据
    每组数据第一行包含一个n(2 ≤ n ≤ 10^5),表示有n座房子
    之后n-1行,每行三个数字a,b,c表示在房子a和房子b之间有一条路,c等于1表示路已被打扫,c等于2表示路未被打扫。

    Output
    每组数据输出一个整数k,表示最少要给k个朋友打电话

    Sample Input
    Original Transformed
    5
    1 2 2
    1 3 2
    1 4 2
    1 5 2
    

    Sample Output
    Original Transformed
    4

    解题思路:

    bfs + dfs

    代码:

    #include <map>
    #include <set>
    #include <stack> 
    #include <queue>
    #include <cstdio>
    #include <string>
    #include <vector>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    using namespace std;
     
    #define mod 7
    #define INF 0x3f3f3f3f
    #define lson (rt << 1)
    #define rson (rt << 1 | 1)
    #define Clear(a) memset(a, 0, sizeof(a))
    #define Max(a, b) ( (a) > (b) ? (a) : (b) )
    #define Min(a, b) ( (a) < (b) ? (a) : (b) )
     
    typedef long long LL;
    typedef pair<int, int > pi;
    typedef struct node{
        int to;
        int w;
        node(){}
        node(int a, int b){
            to = a; w = b;
        }
    }Edge;
    typedef struct nn{
        int lev;
        int id;
    }SS;
     
    const int maxn = 1e5 + 5;
    const int dir[8][2] = {1,2, 2,1, -1,2, -2,1, 1,-2, 2,-1, -1,-2, -2,-1};
     
    int ans;
    SS s[maxn];
    int vis[maxn], num[maxn];
    vector<Edge> vec[maxn];
    bool cmp(SS a, SS b){
        if(a.lev == b.lev){
            return a.id < b.id;
        }else{
            return a.lev > b.lev;
        }
    }
    void dfs(int p){
        vis[p] = 1;
        if(p == 1) return;
        int len = vec[p].size();
        for(int i = 0; i < len; ++i){
            Edge v = vec[p][i];
            if(num[v.to] < num[p])
                dfs(v.to);
        }
    }
    void bfs(){
        queue<int> q;
        memset(s, 0, sizeof(s));
        while(!q.empty()) q.pop();
        q.push(1);
        s[1].id = 1;
        s[1].lev = 1;
         
        while(!q.empty()){
            int now = q.front();
            q.pop();
             
            int len = vec[now].size();
            for(int i = 0; i < len; ++i){
                Edge v = vec[now][i];
                if(!s[v.to].lev){
                    s[v.to].id = v.to;
                    s[v.to].lev = s[now].lev + 1;
                    q.push(v.to);
                }
            }
        }
    }
    int main()
    {
        int n, a, b, c;
        while(~scanf("%d", &n)){
            for(int i = 0; i <= n; ++i) vec[i].clear();
            for(int i = 1; i < n; ++i){
                scanf("%d%d%d", &a, &b, &c);
                 
                vec[a].push_back(node(b, c));
                vec[b].push_back(node(a, c));
            }
             
            bfs();
            for(int i = 1; i <= n; ++i) num[i] = s[i].lev;
            sort(s + 1, s + n + 1, cmp);
            memset(vis, 0, sizeof(vis));
             
            int ans = 0;
            for(int i = 1; i <= n; ++i){
                if(vis[s[i].id]) continue;
                int len = vec[s[i].id].size();
                for(int j = 0; j < len; ++j){
                    Edge tmp = vec[s[i].id][j];
                    if(tmp.w == 2){
                        ++ans;
                        dfs(s[i].id);
                    }
                }
            }
            printf("%d
    ", ans);
        }
        return 0;
    }


  • 相关阅读:
    跑步前后吃什么?
    英雄杀八人场心得
    如何判断JavaScript数据具体类型
    js实现时间日期的格式化
    各个公司前端笔试题回顾
    原型模式Prototype,constructor,__proto__详解
    二级菜单不同方法的实现
    秋招笔试碰到的疑难题目1
    php和mysql学习问题笔记
    es6学习笔记12--Class
  • 原文地址:https://www.cnblogs.com/wiklvrain/p/8179453.html
Copyright © 2011-2022 走看看