Advertisement
Georgiy031

Untitled

Nov 22nd, 2020
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. typedef long long ll;
  5. typedef unsigned long long ull;
  6. #define all(x) x.begin(), x.end()
  7. #define rall(x) x.rbegin(), x.rend()
  8. //#define endl '\n'
  9. #define boostIO() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  10. ll gcd(ll a, ll b) { return (b == 0 ? a : gcd(b, a % b)); }
  11.  
  12. int n;
  13. vector<int> points;
  14. vector <multiset<int>> g;
  15.  
  16. vector<int> path(int v) {
  17.     stack<int> s;
  18.     vector<int> ans;
  19.     s.push(v);
  20.     while (!s.empty()) {
  21.         int w = s.top();
  22.         bool found_edge = false;
  23.         vector<int> ers;
  24.         for (auto u : g[w]) {
  25.             s.push(u);
  26.             ers.push_back(u);
  27.             found_edge = true;
  28.             break;
  29.         }
  30.         for(auto u : ers)
  31.             g[w].erase(g[w].find(u));
  32.  
  33.         if (!found_edge) {
  34.             s.pop();
  35.             ans.push_back(w);
  36.         }
  37.     }
  38.     return ans;
  39. }
  40.  
  41. signed main() {
  42.     int start;
  43.     cin >> n >> start;
  44.     --start;
  45.     points.push_back(start);
  46.     vector<vector<int>> e(n, vector<int>(3));
  47.     for (int i = 0; i < n; ++i) {
  48.         int a, b, c;
  49.         cin >> a >> b >> c;
  50.         --a, --b;
  51.         e[i] = { a, b, c };
  52.         points.push_back(a);
  53.         points.push_back(b);
  54.        
  55.     }
  56.     sort(all(points));
  57.     points.resize(unique(all(points)) - points.begin());
  58.     start = lower_bound(all(points), start) - points.begin();
  59.  
  60.     g.resize(points.size());
  61.     vector<map<int, vector<int>>> q(points.size());
  62.    
  63.     for (int i = 0; i < n; ++i) {
  64.         e[i][0] = lower_bound(all(points), e[i][0]) - points.begin();
  65.         e[i][1] = lower_bound(all(points), e[i][1]) - points.begin();
  66.         if (e[i][2] == 1) {
  67.             g[e[i][0]].insert(e[i][1]);
  68.             q[e[i][0]][e[i][1]].push_back(i);
  69.         }
  70.     }
  71.     auto p = path(start);
  72.     reverse(all(p));
  73.     vector<int> ans1, ans;
  74.     //for (auto& x : p) cout << x + 1 << " ";
  75.     for (int i = 0; i < p.size() - 1; ++i) {
  76.         if (q[p[i]][p[i + 1]].size() == 0) {
  77.             cout << "No";
  78.             return 0;
  79.         }
  80.         ans1.push_back(q[p[i]][p[i + 1]].back());
  81.         q[p[i]][p[i + 1]].pop_back();
  82.     }
  83.     for (int i = 0; i < e.size(); ++i) {
  84.         if (e[i][2] == 0 && e[i][0] != start) ans.push_back(i);
  85.     }
  86.     int t = start;
  87.     int it = 0;
  88.     while (it < ans1.size() && t == start) {
  89.         ans.push_back(ans1[it]);
  90.         t = e[ans.back()][1];
  91.         ++it;
  92.     }
  93.     for (int i = 0; i < e.size(); ++i) {
  94.         if (e[i][2] == 0 && e[i][0] == start) ans.push_back(i);
  95.     }
  96.     for (int i = it; i < ans1.size(); ++i) {
  97.         ans.push_back(ans1[i]);
  98.     }
  99.     if (ans.size() != n) {
  100.         cout << "No";
  101.         return 0;
  102.     }
  103.     for (int i = 0; i < n; ++i) {
  104.         auto& cur = e[ans[i]];
  105.         if (cur[2] == 0) {
  106.             if (start != cur[0]) continue;
  107.             cout << "No";
  108.             return 0;
  109.         }
  110.         else {
  111.             if (start != cur[0]) {
  112.                 cout << "No";
  113.                 return 0;
  114.             }
  115.             start = cur[1];
  116.         }
  117.     }
  118.     cout << "Yes\n";
  119.     for (auto& x : ans) cout << x + 1 << " ";
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement