• [HIHO1174]拓扑排序·一(拓扑排序)


    题目链接:http://hihocoder.com/problemset/problem/1174

    题意:判断一个有向图是否有环,用拓扑排序,结论就是每次取出点的时候统计一下现在剩下几个点,最后没有剩下点就是无环的。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f7f, sizeof(a))
     53 #define lrt rt << 1
     54 #define rrt rt << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef pair<LL, LL> pll;
     65 typedef map<string, int> msi;
     66 typedef vector<int> vi;
     67 typedef vector<LL> vl;
     68 typedef vector<vl> vvl;
     69 typedef vector<bool> vb;
     70 
     71 const int maxn = 100100;
     72 const int maxm = 500500;
     73 typedef struct Edge {
     74     int u, v;
     75     int next;
     76 }Edge;
     77 Edge edge[maxm];
     78 int ecnt, head[maxn];
     79 int n, m;
     80 int dig[maxn];
     81 priority_queue<int, vector<int>, greater<int> > pq;
     82 
     83 void adde(int u, int v) {
     84     edge[ecnt].u = u;
     85     edge[ecnt].v = v;
     86     edge[ecnt].next = head[u];
     87     head[u] = ecnt++;
     88 }
     89 
     90 int main() {
     91     // FRead();
     92     int T, u, v;
     93     Rint(T);
     94     W(T) {
     95         ecnt = 0; Clr(head, -1); Cls(dig);
     96         Rint(n); Rint(m);
     97         Rep(i, m) {
     98             Rint(u); Rint(v);
     99             adde(u, v);
    100             dig[v]++;
    101         }
    102         while(!pq.empty()) pq.pop();
    103         For(i, 1, n+1) if(dig[i] == 0) pq.push(i);
    104         int cnt = n;
    105         while(!pq.empty()) {
    106             u = pq.top(); pq.pop();
    107             cnt--;
    108             for(int i = head[u]; ~i; i=edge[i].next) {
    109                 v = edge[i].v;
    110                 if(--dig[v] == 0) pq.push(v);
    111             }
    112         }
    113         if(cnt != 0) printf("Wrong
    ");
    114         else printf("Correct
    ");
    115     }
    116     RT 0;
    117 }
  • 相关阅读:
    系统设计题:如何设计一个电商平台积分兑换系统!
    服务器上部署多台mysql
    log4j日志输出格式一览
    Intellij IDEA 智能补全
    什么是旅行商问题——算法NP、P、NPC知识
    如何找到两个升序数组归并后的升序数组的中位数
    Java 不同进制的字面值
    Android 进程和线程
    美图秀秀2015年实习生android应用开发方向招聘笔试题
    Android:Layout_weight的深刻理解
  • 原文地址:https://www.cnblogs.com/kirai/p/5580300.html
Copyright © 2020-2023  润新知