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
    **********************************************************************/
    
    
  • 相关阅读:
    LeetCode 404. 左叶子之和
    三年了
    LeetCode 543. 二叉树的直径
    求结点在二叉排序树中层次的算法
    LeetCode 98. 验证二叉搜索树
    LeetCode 236. 二叉树的最近公共祖先
    LeetCode 129. 求根到叶子节点数字之和
    LeetCode 113. 路径总和 II
    LeetCode 107. 二叉树的层次遍历 II
    LeetCode 144. 二叉树的前序遍历 (非递归)
  • 原文地址:https://www.cnblogs.com/artoriax/p/10351639.html
Copyright © 2011-2022 走看看