Advertisement
Nik_Perepelov

7 контест Надару

Nov 3rd, 2021
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. //1
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5. vector<int> ordr, actual_comp;
  6. vector<bool> visited;
  7. vector<vector<int>> graph, reversed_graph;
  8.  
  9. void dfs2(int vertex) {
  10.     visited[vertex] = true;
  11.     actual_comp.push_back(vertex);
  12.     for (int i = 0; i < reversed_graph[vertex].size(); i++)
  13.         if (!visited[reversed_graph[vertex][i]])
  14.             dfs2(reversed_graph[vertex][i]);
  15. }
  16. void dfs1(int vertex) {
  17.     visited[vertex] = true;
  18.     for (int i = 0; i < graph[vertex].size(); i++)
  19.         if (!visited[graph[vertex][i]])
  20.             dfs1(graph[vertex][i]);
  21.     ordr.push_back(vertex);
  22. }
  23.  
  24.  
  25.  
  26. int main() {
  27.     int num_of_vertex, num_of_edges, a, b;
  28.     cin >> num_of_vertex >> num_of_edges;
  29.     visited.resize(num_of_vertex);
  30.     reversed_graph = vector<vector<int>> (num_of_vertex);
  31.     graph = vector<vector<int>> (num_of_vertex);
  32.     for (int i = 0; i < num_of_edges; i++) {
  33.         cin >> a >> b;
  34.         reversed_graph[b - 1].push_back(a - 1);
  35.         graph[a - 1].push_back(b - 1);
  36.     }
  37.     for (int i = 0; i < num_of_vertex; i++)
  38.         if (!visited[i])
  39.             dfs1(i);
  40.     visited.assign(num_of_vertex, false);
  41.  
  42.     vector<int> answer(num_of_vertex);
  43.     int num_of_components = 0;
  44.     for (int i = 0; i < num_of_vertex; i++) {
  45.         if (visited[ordr[num_of_vertex - i - 1]] == false) {
  46.             dfs2(ordr[num_of_vertex - 1 - i]);
  47.             for (int k = 0; k < actual_comp.size(); k++)
  48.                 answer[actual_comp[k]] = num_of_components + 1;
  49.             num_of_components+=1;
  50.             actual_comp.clear();
  51.         }
  52.     }
  53.     cout << num_of_components << endl;
  54.     for (int i = 0; i < num_of_vertex; i++) {
  55.         cout << answer[i]<< " ";
  56.     }
  57. }
  58.  
  59. // 2
  60.  
  61. #include <iostream>
  62. #include <vector>
  63. using namespace std;
  64. vector<int> ordr, actual_comp;
  65. vector<int> visited;
  66. vector<vector<int>> graph, reversed_graph;
  67. void dfs1(int vertex) {
  68.     visited[vertex] = 1;
  69.     for (int i = 0; i < graph[vertex].size(); i++)
  70.         if (!visited[graph[vertex][i]])
  71.             dfs1(graph[vertex][i]);
  72.     ordr.push_back(vertex);
  73. }
  74. void dfs2(int vertex) {
  75.     visited[vertex] = 1;
  76.     actual_comp.push_back(vertex);
  77.     for (int i = 0; i < reversed_graph[vertex].size(); i++)
  78.         if (!visited[reversed_graph[vertex][i]])
  79.             dfs2(reversed_graph[vertex][i]);
  80. }
  81. int main() {
  82.     int num_of_vertex, num_of_edges;
  83.     cin >> num_of_vertex >> num_of_edges;
  84.     visited.resize(num_of_vertex);
  85.     reversed_graph = vector<vector<int>> (num_of_vertex);
  86.     vector<pair<int,int>> edges;
  87.     vector<int> final;
  88.     graph = vector<vector<int>> (num_of_vertex);
  89.     for (int i = 0; i < num_of_edges; i++) {
  90.         int f;
  91.         int t;
  92.         cin >> f >> t;
  93.         reversed_graph[t - 1].push_back(f - 1);
  94.         graph[f - 1].push_back(t - 1);
  95.         pair<int,int> tmp(f - 1, t - 1);
  96.         edges.push_back(tmp);
  97.     }
  98.     for (int i = 0; i < num_of_vertex; i++)
  99.         if (visited[i] == 0)
  100.             dfs1(i);
  101.     visited.assign(num_of_vertex, 0);
  102.  
  103.     vector<int> answer(num_of_vertex);
  104.     int num_of_components = 0;
  105.     for (int i = 0; i < num_of_vertex; i++) {
  106.         if (visited[ordr[num_of_vertex - i - 1]] == false) {
  107.             dfs2(ordr[num_of_vertex - 1 - i]);
  108.             for (int k = 0; k < actual_comp.size(); k++)
  109.                 answer[actual_comp[k]] = num_of_components + 1;
  110.             num_of_components+=1;
  111.             actual_comp.clear();
  112.         }
  113.     }
  114.     vector<int> firebase(num_of_components);
  115.     for (int i = 0; i < num_of_edges; i++){
  116.         if (answer[edges[i].first] != answer[edges[i].second]){
  117.             firebase[answer[edges[i].first] - 1] = 1;
  118.         }
  119.     }
  120.     for (int i = 0; i < num_of_components; i++){
  121.         if (firebase[i] == 0){
  122.             for (int k = 0; k < num_of_vertex; k++){
  123.                 if (answer[k] == i + 1){
  124.                     final.emplace_back(k + 1);
  125.                     break;
  126.                 }
  127.             }
  128.         }
  129.     }
  130.     cout << final.size() << endl;
  131.     for (int i = 0; i < final.size(); i++){
  132.         cout << final[i] << '\n';
  133.     }
  134. }
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement