zoukankan      html  css  js  c++  java
  • codeforces 129B students and shoes

    https://vjudge.net/problem/CodeForces-129B

    题意:

    有n个学生,他们之间被鞋带缠住了。现在,老师首先把所有只与一个学生直接相连的学生找出来,让他们聚集到一起,然后把他们踢出去,直到无人可踢为止。问可以踢多少次。

    思路:

    用拓扑排序的思路,从所有点的度数下手。将所有度数为1的点入队,之后把他们连接的点的度数全部减一,自己也减1,之后再把所有的度数为1的点入队,循环,直到队列为空。

    代码:

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <queue>
     4 #include <vector>
     5 using namespace std;
     6 
     7 int d[105];
     8 
     9 queue<int> q;
    10 vector<int> v[105];
    11 
    12 int main()
    13 {
    14     int n,m;
    15 
    16     scanf("%d%d",&n,&m);
    17 
    18     for (int i = 0;i < m;i++)
    19     {
    20         int a,b;
    21 
    22         scanf("%d%d",&a,&b);
    23 
    24         d[a]++;d[b]++;
    25 
    26         v[a].push_back(b);
    27         v[b].push_back(a);
    28     }
    29 
    30     for (int i = 1;i <= n;i++)
    31     {
    32         if (d[i] == 1) q.push(i);
    33     }
    34 
    35     int ans = 0;
    36 
    37     while (!q.empty())
    38     {
    39         int cnt = 0;
    40 
    41         int b[105];
    42 
    43         while (!q.empty())
    44         {
    45             b[cnt++] = q.front();q.pop();
    46         }
    47 
    48         if (cnt > 0) ans++;
    49 
    50         for (int i = 0;i < cnt;i++)
    51         {
    52             int t = b[i];
    53             d[t]--;
    54 
    55             for (int j = 0;j < v[t].size();j++)
    56             {
    57                 int s = v[t][j];
    58 
    59                 if (d[s] >= 1)
    60                 {
    61                     d[s]--;
    62                 }
    63             }
    64         }
    65 
    66         for (int i = 1;i <= n;i++)
    67         {
    68             if (d[i] == 1) q.push(i);
    69         }
    70     }
    71 
    72     printf("%d
    ",ans);
    73 
    74     return 0;
    75 }
  • 相关阅读:
    time 模块学习
    day 14 自定义模块,常用模块 time .datetime ,time 模块
    day 13 课后作业
    day 12 课后作业
    day 11课后作业
    树状数组最值
    hdu 1059 Dividing bitset 多重背包
    XVII Open Cup named after E.V. Pankratiev. XXI Ural Championship
    最长公共子序列板/滚动 N^2
    Uva 10635
  • 原文地址:https://www.cnblogs.com/kickit/p/7209522.html
Copyright © 2011-2022 走看看