Advertisement
erek1e

Interference - BIO 2024 Round 2

Apr 11th, 2024
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. /**
  2.  * @file interference.cpp
  3.  * @version 4.0 (of problem statement)
  4.  * @date 2024-04-01
  5.  *
  6.  * usage:
  7.  *      Read from / write to default input.txt and output.txt
  8.  *          interference.exe
  9.  *      Read from / write to custom files:
  10.  *          interference.exe in.txt out.txt
  11.  */
  12. #include <iostream>
  13. #include <vector>
  14. #include <functional>
  15. #include <cassert>
  16.  
  17. using namespace std;
  18.  
  19. void fileIO(int argc, char *argv[]);
  20.  
  21. int main(int argc, char *argv[]) {
  22.     fileIO(argc, argv); // remove for standard input/output
  23.    
  24.     int s, d; cin >> s >> d;
  25.     vector<int> f(1+s);
  26.     // set f to 3 for all nodes in disrepair
  27.     for (int i = 0; i < d; ++i) {
  28.         int disrepair; cin >> disrepair;
  29.         f[disrepair] = 3;
  30.     }
  31.  
  32.     vector<vector<pair<int, bool>>> g(1+s);
  33.     for (int u, v, w; cin >> u >> v >> w && u != -1; ) {
  34.         g[u].emplace_back(v, w);
  35.         g[v].emplace_back(u, w);
  36.     }
  37.  
  38.     function<void(int, int)> dfsSetup = [&](int node, int parent = 0) {
  39.         f[node] = 1;
  40.         for (auto [child, far] : g[node]) {
  41.             if (child != parent) {
  42.                 if (f[child]) {
  43.                     assert(f[child] == 3);
  44.                     if (!far) f[node] = 2;
  45.                 } else {
  46.                     dfsSetup(child, node);
  47.                     assert(f[child] < 3); // roots are always assigned values < 3, we will flip the subtrees later if we want this to be a far edge
  48.                 }
  49.             }
  50.         }
  51.     };
  52.     function<void(int, int, bool)> dfsCorrect = [&](int node, int parent = 0, bool flipSubtree = false) {
  53.         for (auto [child, far] : g[node]) {
  54.             if (child != parent) {
  55.                 assert(f[child]);
  56.                 if (f[child] != 3) dfsCorrect(child, node, flipSubtree ^ far);
  57.             }
  58.         }
  59.         if (flipSubtree) f[node] = 6 - f[node];
  60.     };
  61.  
  62.     for (int i = 1; i <= s; ++i) {
  63.         if (!f[i]) {
  64.             dfsSetup(i, 0);
  65.             dfsCorrect(i, 0, false);
  66.         }
  67.         cout << f[i] << '\n';
  68.     }
  69.     return 0;
  70. }
  71.  
  72. void fileIO(int argc, char *argv[]) {
  73.     const char * in = "input.txt", * out = "output.txt";
  74.     if (argc > 1) in = argv[1];
  75.     if (argc > 2) out = argv[2];
  76.     freopen(in, "r", stdin);
  77.     freopen(out, "w", stdout);
  78. }
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement