zoukankan      html  css  js  c++  java
  • HDU4496-D-City

    题目链接:点击打开链接

    Problem Description

    Luxer is a really bad guy. He destroys everything he met.
    One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input.
    Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.

    Input

    First line of the input contains two integers N and M.
    Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line.
    Constraints:
    0 < N <= 10000
    0 < M <= 100000
    0 <= u, v < N.

    Output

    Output M lines, the ith line is the answer after deleting the first i edges in the input.

    Sample Input

     

    5 10 0 1 1 2 1 3 1 4 0 2 2 3 0 4 0 3 3 4 2 4

    Sample Output

     

    1 1 1 2 2 2 2 3 4 5

    思路:题意就是 一个人把一个连通的图,一条一条路慢慢地破坏,每破坏一条路,看是否有顶点被独立出来。我按照这个思路用一个数组储存了所有点的入度。最后遍历,每遍历一条路,路的端点减1;下面是这个思路的代码(不是超时就是wrong)

    代码:

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<string>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstdlib>
    #include<cctype>
    #include<cstring>
    using namespace std;
    const int MAX = 10010;
    const int INF = 0X3f3f3f;
    
    int indegree[MAX];
    
    
    int main() {
    	int n, m;
    	while(scanf("%d%d", &n, &m) != EOF) {
            vector<pair<int, int> > t;
            int u, v;
            memset(indegree, 0, sizeof(indegree));
            int ans = 1;
            while(m--) {
                scanf("%d%d", &u, &v);
                t.push_back(make_pair(u,v));
                indegree[u]++;
                indegree[v]++;
            }
            for(int i = 0; i < t.size(); i++) {
                int u = t[i].first;
                int v = t[i].second;
                indegree[u]--;
                if(indegree[u] == 0) {
                    ans++;
                }
                indegree[v]--;
                if(indegree[v] == 0) {
                    ans++;
                }
                if(i == t.size() - 1)
                    printf("%d
    ", ans - 1);
                else
                    printf("%d
    ", ans);
            }
    	}
    	return 0;
    }

    后来看了题解。用的逆序并查集。先把所有点当作未连通的, 每处理一条路(倒着处理,但是第一条《输入时候的第一条不作处理》), 把连通块输出。

    AC代码:

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<stack>
    #include<string>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstdlib>
    #include<cctype>
    #include<cstring>
    using namespace std;
    const int MAX = 10010;
    const int INF = 0X3f3f3f;
    
    int father[MAX];
    int n, m;
    int sum;
    void init() {//初始化
        for(int i = 0; i < n; i++) {
            father[i] = i;
        }
    }
    int findfather(int x) { //路径压缩
        int a = x;
        while(x != father[x]) {
            x = father[x];
        }
        while(a != father[a]) {
            int z = a;
            a = father[a];
            father[z] = x;
        }
        return x;
    }
    
    void Union(int a, int b) {//合并
        int faA = findfather(a);
        int faB = findfather(b);
        if(faA != faB) {
            sum--;//如果不是一个集合 就说明原来不连通的点 少了一个
            father[faA] = faB;
        }
    }
    
    int main() {
        int u, v;
        while(scanf("%d%d", &n, &m) != EOF) {
            init();
            sum = n;
            vector<pair<int, int> > t;//存路,以后遍历用
            vector<int> tt;//存输出数据
            tt.push_back(n);//先把最初的不连通  存进去;
            while(m--) {
                scanf("%d%d", &u, &v);
                t.push_back(make_pair(u, v));
            }
            for(int i = t.size() - 1; i > 0; i--) {//遍历路径  注意 i > 0, 而不是 i >= 0;
                int u = t[i].first;
                int v = t[i].second;
                Union(u, v);
                tt.push_back(sum);
            }
            for(int i = tt.size() - 1; i >= 0; i--) {//倒着输出
                printf("%d
    ", tt[i]);
            }
        }
    	return 0;
    }
  • 相关阅读:
    C# 查找其他应用程序并打开、显示、隐藏、关闭的API
    微信公众平台开发2-access_token获取及应用(含源码)
    Winform下编译Dev控件时提示license.licx文件错误
    JS+MySQL获取 京东 省市区 地区
    MySQL性能调优与架构设计——第11章 常用存储引擎优化
    MySQL性能调优与架构设计——第10章 MySQL数据库Schema设计的性能优化
    MySQL性能调优与架构设计——第9章 MySQL数据库Schema设计的性能优化
    MySQL性能调优与架构设计——第1章 MySQL 基本介绍
    .NET基础 (21)ASP NET应用开发
    .NET基础 (20).NET中的数据库开发
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9572997.html
Copyright © 2011-2022 走看看