Nik_Perepelov

Доп контест Ксюше

Nov 17th, 2021 (edited)
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.11 KB | None | 0 0
  1. // 1 задача
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. class Task {
  8.     vector<int> parent;
  9.     vector<int> my_rank;
  10.     vector<vector<int>> values;
  11. public:
  12.  
  13.     Task(int n) {
  14.         parent.resize(n);
  15.         my_rank.resize(n);
  16.         values.resize(n);
  17.     }
  18.  
  19.     void make_set(int x) {
  20.         parent[x] = x;
  21.         my_rank[x] = 0;
  22.         values[x].push_back(x + 1);
  23.     }
  24.  
  25.     int find_set(int x) {
  26.         if (x == parent[x])
  27.             return x;
  28.         return find_set(parent[x]);
  29.     }
  30.  
  31.     void union_sets(int first_cage, int second_cage) {
  32.         first_cage = find_set(first_cage);
  33.         second_cage = find_set(second_cage);
  34.         if (first_cage != second_cage) {
  35.             if (my_rank[first_cage] < my_rank[second_cage])
  36.                 swap(first_cage, second_cage);
  37.  
  38.             // доабвляем котят в большую клетку
  39.             values[first_cage].insert(values[first_cage].end(), values[second_cage].begin(), values[second_cage].end());
  40.  
  41.             parent[second_cage] = first_cage;
  42.             if (my_rank[first_cage] == my_rank[second_cage])
  43.                 my_rank[first_cage]++;
  44.  
  45.         }
  46.     }
  47.  
  48.     void print_answer() {
  49.         for (int i : values[find_set(0)]) {
  50.             cout << i;
  51.             cout << ' ';
  52.         }
  53.     }
  54. };
  55.  
  56. int main() {
  57.     int n;
  58.     cin >> n;
  59.     Task t(n);
  60.     for (int i = 0; i < n; i++)
  61.         t.make_set(i);
  62.     for (int i = 0; i < n; i++) {
  63.         int from, to;
  64.         cin >> from >> to;
  65.         from -= 1;
  66.         to -= 1;
  67.         t.union_sets(from, to);
  68.     }
  69.     t.print_answer();
  70.  
  71. }
  72.  
  73. // 2 задача
  74.  
  75. #include <iostream>
  76. #include <vector>
  77.  
  78. using namespace std;
  79.  
  80. class Task {
  81.     vector<int> parent;
  82.     vector<int> my_rank;
  83.     vector<string> query_type;
  84.     vector<int> query_a;
  85.     vector<int> query_b;
  86.     vector<string> answers;
  87. public:
  88.  
  89.     Task(int n) {
  90.         parent.resize(n);
  91.         my_rank.resize(n);
  92.         for (int i = 0; i < n; i++)
  93.             make_set(i);
  94.     }
  95.  
  96.     void make_set(int x) {
  97.         parent[x] = x;
  98.         my_rank[x] = 0;
  99.     }
  100.  
  101.     int find_set(int x) {
  102.         if (x == parent[x])
  103.             return x;
  104.         return find_set(parent[x]);
  105.     }
  106.  
  107.     void union_sets(int first_cage, int second_cage) {
  108.         first_cage = find_set(first_cage);
  109.         second_cage = find_set(second_cage);
  110.         if (first_cage != second_cage) {
  111.             if (my_rank[first_cage] < my_rank[second_cage])
  112.                 swap(first_cage, second_cage);
  113.  
  114.             parent[second_cage] = first_cage;
  115.             if (my_rank[first_cage] == my_rank[second_cage])
  116.                 my_rank[first_cage]++;
  117.  
  118.         }
  119.     }
  120.  
  121.     void read_graph(int m) {
  122.         int kek;
  123.         for (int i = 0; i < m; i++)
  124.             cin >> kek;
  125.         for (int i = 0; i < m; i++)
  126.             cin >> kek;
  127.     }
  128.  
  129.     void read_queries(int k){
  130.         query_type.resize(k);
  131.         query_a.resize(k);
  132.         query_b.resize(k);
  133.         for (int i = 0; i < k; i++){
  134.             cin >> query_type[i] >> query_a[i] >> query_b[i];
  135.             query_a[i]--;
  136.             query_b[i]--;
  137.         }
  138.     }
  139.  
  140.     void process_queries(int k){
  141.         for (int i = k - 1; i >= 0; i--){
  142.             if (query_type[i] == "cut"){
  143.                 union_sets(query_a[i], query_b[i]);
  144.             } else {
  145.                 if (find_set(query_a[i]) == find_set(query_b[i]))
  146.                     answers.emplace_back("YES");
  147.                 else
  148.                     answers.emplace_back("NO");
  149.             }
  150.         }
  151.     }
  152.  
  153.     void print_answers(){
  154.         for (int i = 0; i < answers.size(); i++)
  155.             cout << answers[answers.size() - 1 - i] << endl;
  156.     }
  157. };
  158.  
  159. int main() {
  160.     int n, m, k;
  161.     cin >> n >> m >> k;
  162.  
  163.     Task t(n);
  164.     t.read_graph(m);
  165.     t.read_queries(k);
  166.     t.process_queries(k);
  167.     t.print_answers();
  168. }
  169.  
  170. // 3 задача
  171.  
  172. #include <iostream>
  173. #include <vector>
  174.  
  175. using namespace std;
  176.  
  177. class Task {
  178.     int n, m;
  179.     vector<int> used;
  180.     vector<vector<int>> th;
  181.     vector<int> order;
  182.    
  183.     void end_task(){
  184.         cout << "No";
  185.         exit(0);
  186.     }
  187.  
  188.     void dfs(int v, int p) {
  189.         used[v] = 1;
  190.         for (auto &i: th[v]) {
  191.             if (used[i] == 1) {
  192.                 end_task();
  193.             }
  194.             if (used[i] == 0) {
  195.                 dfs(i, v);
  196.             }
  197.         }
  198.         used[v] = 2;
  199.         order.push_back(v);
  200.     }
  201. public:
  202.  
  203.     Task(int _n, int _m){
  204.         n = _n;
  205.         m = _m;
  206.         used.resize(n);
  207.         th.resize(n);
  208.         // order.resize(n);
  209.  
  210.     }
  211.  
  212.     void read_graph(){
  213.         for (int i = 0; i < m; i++){
  214.             int a, b;
  215.             cin >> a >> b;
  216.             a--; b--;
  217.             th[a].push_back(b);
  218.         }
  219.     }
  220.  
  221.  
  222.     void solve(){
  223.         for (int i = 0; i < n; i++){
  224.             if (!used[i])
  225.                 dfs(i, -1);
  226.         }
  227.         cout << "Yes" << endl;
  228.         for (int i = 0; i < n; i++){
  229.             cout << order[n - 1 - i] + 1 << " ";
  230.         }
  231.     }
  232.  
  233. };
  234. int main() {
  235.     int n, m;
  236.     cin >> n >> m;
  237.  
  238.     Task t(n, m);
  239.     t.read_graph();
  240.     t.solve();
  241.  
  242.     return 0;
  243. }
  244.  
  245. // 4 задача
  246. #include <iostream>
  247. #include <vector>
  248.  
  249. using namespace std;
  250.  
  251. class Task {
  252.     vector<int> parent;
  253.     vector<int> my_rank;
  254.     int c = 0;
  255. public:
  256.  
  257.     Task(int n) {
  258.         parent.resize(n);
  259.         my_rank.resize(n);
  260.         for (int i = 0; i < n; i++)
  261.             make_set(i);
  262.         c = n;
  263.     }
  264.  
  265.     void make_set(int x) {
  266.         parent[x] = x;
  267.         my_rank[x] = 0;
  268.     }
  269.  
  270.     int find_set(int x) {
  271.         if (x == parent[x])
  272.             return x;
  273.         return find_set(parent[x]);
  274.     }
  275.  
  276.     void union_sets(int first_cage, int second_cage) {
  277.         first_cage = find_set(first_cage);
  278.         second_cage = find_set(second_cage);
  279.         if (first_cage != second_cage) {
  280.             if (my_rank[first_cage] < my_rank[second_cage])
  281.                 swap(first_cage, second_cage);
  282.  
  283.             parent[second_cage] = first_cage;
  284.             if (my_rank[first_cage] == my_rank[second_cage])
  285.                 my_rank[first_cage]++;
  286.             c--;
  287.         }
  288.     }
  289.     int get_c(){
  290.         return c;
  291.     }
  292. };
  293.  
  294. int main() {
  295.     int n;
  296.     cin >> n;
  297.  
  298.     Task t(n);
  299.     for (int i = 0; i < n; i++){
  300.         int key;
  301.         cin >> key;
  302.         key--;
  303.         t.union_sets(i, key);
  304.     }
  305.  
  306.     cout << t.get_c();
  307.  
  308. }
  309.  
  310. // 7 задача
  311. #include <iostream>
  312. #include <vector>
  313. #include <limits.h>
  314.  
  315. using namespace std;
  316.  
  317. struct Edge {
  318.     int to;
  319.     int departing_time;
  320.     int arrival_time;
  321. };
  322.  
  323. class Task {
  324.  
  325.     int n;
  326.  
  327.     vector<int> used;
  328.  
  329.     vector<vector<Edge>> th;
  330.  
  331.     vector<int> th_time;
  332.  
  333. public:
  334.  
  335.     Task(int _n) {
  336.         n = _n;
  337.         th.resize(n);
  338.         used.resize(n);
  339.         th_time.resize(n, INT_MAX);
  340.     }
  341.  
  342.     void read_graph(int m) {
  343.         for (int i = 0; i < m; i++) {
  344.             int k;
  345.             cin >> k;
  346.  
  347.             int from, to, from_t, to_t;
  348.             for (int j = 0; j < k; j++) {
  349.                 cin >> to >> to_t;
  350.                 to-=1;
  351.                 if (j > 0) {
  352.                     th[from].push_back(Edge{to, from_t, to_t});
  353.                 }
  354.                 from = to;
  355.                 from_t = to_t;
  356.             }
  357.         }
  358.     }
  359.  
  360.     void dijkstra() {
  361.         for (;;) {
  362.             int min_v = -1, min_dist = INT_MAX;
  363.             for (int i = 0; i < n; i++) {
  364.                 if (!used[i] && th_time[i] < min_dist) {
  365.                     min_v = i;
  366.                     min_dist = th_time[i];
  367.                 }
  368.             }
  369.             if (min_v == -1)
  370.                 return;
  371.             used[min_v] = true;
  372.             for (auto &i: th[min_v]) {
  373.                 if (i.departing_time >= th_time[min_v] && i.arrival_time < th_time[i.to])
  374.                     th_time[i.to] = i.arrival_time;
  375.             }
  376.         }
  377.     }
  378.  
  379.     void solve(int start) {
  380.         th_time[start] = 0;
  381.         dijkstra();
  382.     }
  383.  
  384.     void print_result(int finish) {
  385.         if (th_time[finish] != INT_MAX)
  386.             cout << th_time[finish];
  387.         else
  388.             cout << "-1";
  389.     }
  390. };
  391.  
  392. int main() {
  393.     int n, m, finish;
  394.     cin >> n >> finish >> m;
  395.  
  396.     Task t(n);
  397.     t.read_graph(m);
  398.     t.solve(0);
  399.     t.print_result(finish - 1);
  400.  
  401.  
  402.     return 0;
  403. }
  404.  
  405. // 8 задача
  406.  
  407. #include <iostream>
  408. #include <vector>
  409. #include <limits.h>
  410.  
  411. using namespace std;
  412.  
  413. struct Edge {
  414.     int to;
  415.     int departing_time;
  416.     int arrival_time;
  417. };
  418.  
  419. class Task {
  420.  
  421.     int n;
  422.  
  423.     vector<int> used;
  424.  
  425.     vector<vector<Edge>> th;
  426.  
  427.     vector<int> th_time;
  428.  
  429. public:
  430.  
  431.     Task(int _n) {
  432.         n = _n;
  433.         th.resize(n);
  434.         used.resize(n);
  435.         th_time.resize(n, INT_MAX);
  436.     }
  437.  
  438.     void read_graph(int m) {
  439.         for (int i = 0; i < m; i++) {
  440.             int from, from_t, to, to_t;
  441.             cin >> from >> from_t >> to >> to_t;
  442.             th[from - 1].push_back(Edge{to - 1, from_t, to_t});
  443.         }
  444.     }
  445.  
  446.     void dijkstra() {
  447.         for (;;) {
  448.             int min_v = -1, min_dist = INT_MAX;
  449.             for (int i = 0; i < n; i++) {
  450.                 if (!used[i] && th_time[i] < min_dist) {
  451.                     min_v = i;
  452.                     min_dist = th_time[i];
  453.                 }
  454.             }
  455.             if (min_v == -1)
  456.                 return;
  457.             used[min_v] = true;
  458.             for (auto &i: th[min_v]) {
  459.                 if (i.departing_time >= th_time[min_v] && i.arrival_time < th_time[i.to])
  460.                     th_time[i.to] = i.arrival_time;
  461.             }
  462.         }
  463.     }
  464.  
  465.     void solve(int start) {
  466.         th_time[start] = 0;
  467.         dijkstra();
  468.     }
  469.  
  470.     void print_result(int finish) {
  471.         if (th_time[finish] != INT_MAX)
  472.             cout << th_time[finish];
  473.         else
  474.             cout << "-1";
  475.     }
  476. };
  477.  
  478. int main() {
  479.     int n, m, finish, start;
  480.     cin >> n >> start >> finish >> m;
  481.  
  482.     Task t(n);
  483.     t.read_graph(m);
  484.     t.solve(start - 1);
  485.     t.print_result(finish - 1);
  486.  
  487.  
  488.     return 0;
  489. }
Add Comment
Please, Sign In to add comment