zoukankan      html  css  js  c++  java
  • CSU-1908 The Big Escape

    CSU-1908 The Big Escape

    Description

    There is a tree-like prison. Expect the root node, each node has a prisoner, and the root node is the only exit. Each node can accommodate a large number of prisoners, but each edge per minute only one prisoner can pass.
    Now, the big escape begins, every prisoner wants to escape to the exit.Do you know when the last one escapes from the prison.

    Input

    There are lots of case.
    For each test case.The first line contains two integers n,m(n<=100000, 1<=m<=n), which indicates the number of nodes and the root node.
    The next n-1 lines describe the tree.

    Output

    For each test case, you output one line “Case #%d:%d”

    Sample Input

    10 2
    1 2
    2 3
    2 4
    2 5
    1 6
    5 7
    3 8
    2 9
    2 10
    

    Sample Output

    Case #1:2
    

    题解

    题意是给定一个树形监狱,每条边每分钟只能允许一个人通过,给定根节点,犯人逃到根节点就算逃出,每个节点可以存在多个犯人,问逃出的最短时间

    这个题就是统计根节点最大子树有多少节点

    #include<bits/stdc++.h>
    #define maxn 100050
    using namespace std;
    vector<int> G[maxn];
    int sum;
    void dfs(int u, int fa) {
    	for (int i = 0; i < G[u].size(); i++) {
    		int v = G[u][i];
    		if (v == fa) continue;
    		sum++;
    		dfs(v, u);
    	}
    }
    int main() {
    	int n, m;
    	int cnt = 0;
    	while (scanf("%d%d", &n, &m) != EOF) {
    		cnt++;
    		for (int i = 1; i <= 100000; i++) {
    			G[i].clear();
    		}
    		for (int i = 1; i < n; i++) {
    			int a, b;
    			scanf("%d%d", &a, &b);
    			G[a].push_back(b);
    			G[b].push_back(a);
    		}
    		int ans = 0;
    		for (int i = 0; i < G[m].size(); i++) {
    			int v = G[m][i];
    			sum = 0;
    			dfs(v, m);
    			ans = max(ans, sum);
    		}
    		printf("Case #%d:%d
    ", cnt, ans + 1);
    	}
    }
    /**********************************************************************
    	Problem: 1908
    	User: Artoriax
    	Language: C++
    	Result: AC
    	Time:748 ms
    	Memory:8064 kb
    **********************************************************************/
    
    
  • 相关阅读:
    Heapsort 堆排序算法详解(Java实现)
    GIve Me A Welcome Hug!
    linux系统救援模式拯救mv libc.so.6文件后无法使用命令的悲剧
    RabbitMQ集群部署
    使用Xshell通过堡垒机登录服务器
    dubbo + zookeeper环境部署
    zookeeper集群部署
    zabbix-3.0.1 添加微信报警
    zabbix-3.0.1结合grafana绘图
    Centos7.2安装zabbix3.0.1简要
  • 原文地址:https://www.cnblogs.com/artoriax/p/10351639.html
Copyright © 2011-2022 走看看