HDU 2094 产生冠军 (拓扑排序思想)

    xiaoxiao2024-10-15  81

    本题虽然不需要拓扑排序,但是思想和拓扑排序是一样的,拓扑排序每次是弹出入度为0的点,且不能有环, 同样,这道题只要存在唯一的入度为0的点,就解决了

    下面是AC代码:

    #include<bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int main() { int n; while (cin >> n && n) { map <string, int> e; // 存id + 入度 set <string> cnt; // 存id for (int i = 0; i < n; i++) { string ui, vi; cin >> ui >> vi; e[vi]++; cnt.insert(ui), cnt.insert(vi); } int tot = 0; for (auto it : cnt) { if (e[it] == 0) tot++; } if (tot == 1) cout << "Yes" << endl; // 入度为0且唯一 else cout << "No" << endl; } return 0; }

     

    最新回复(0)