Advertisement
willy108

countcross silver

Jan 8th, 2021
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <map>
  9. #include <stack>
  10. #include <bitset>
  11. #include <queue>
  12. #include <unordered_map>
  13. #include <set>
  14. #include <string>
  15. #include <sstream>
  16. #include <functional>
  17. #include <list>
  18. #include <fstream>
  19.  
  20. #define cont continue
  21. #define int_max 0x3f3f3f3f
  22. #define byte_max 0x3f
  23. #define max_v 110
  24. #define pow_2(n) (1 << n)
  25.  
  26. #define cin fin
  27. #define cout fout
  28.  
  29. using namespace std;
  30.  
  31. ifstream fin("countcross.in");
  32. ofstream fout("countcross.out");
  33.  
  34. const int row[] = {0, 0, -1, 1};
  35. const int col[] = {1, -1, 0, 0};
  36.  
  37.  
  38. int graph[max_v][max_v], belong[max_v][max_v];
  39. bool cow[max_v][max_v], vis[max_v][max_v];
  40. int N, V, E;
  41. vector<int> regions;
  42.  
  43. void add_edge(int a, int b, int c, int d){
  44.     int x = c - a;
  45.     int y = d - b;
  46.     int ind;
  47.     for(int i = 0; i<4; i++){
  48.         if(row[i] == x && col[i] == y){
  49.             ind = i;
  50.         }
  51.     }
  52.    
  53.    
  54.     graph[a][b] |= pow_2(ind);
  55. }
  56.  
  57. int dfs(int x, int y){
  58.    
  59.     if(x < 1 || y < 1 || x > N || y > N || vis[x][y]){ //one based index
  60.         return 0;
  61.     }
  62.    
  63.    
  64.     vis[x][y] = true;
  65.     belong[x][y] = regions.size();
  66.    
  67.    
  68.     int total = (cow[x][y]) ? 1:0;
  69.    
  70.    
  71.     for(int i = 0; i<4; i++){
  72.         if((graph[x][y]&pow_2(i)) == 0){
  73.             total += dfs(x + row[i], y + col[i]);
  74.         }
  75.     }
  76.    
  77.     return total;
  78. }
  79.  
  80. int main() {
  81.     cin >> N >> V >> E;
  82.    
  83.     for(int i = 0; i<E; i++){
  84.         int a, b, c, d;
  85.         cin >> a >> b >> c >> d;
  86.         add_edge(a, b, c, d);
  87.         add_edge(c, d, a, b);
  88.     }
  89.    
  90.     for(int i = 0; i<V; i++){
  91.         int a, b;
  92.         cin >> a >> b;
  93.         cow[a][b] = true;
  94.     }
  95.    
  96.     for(int i = 1; i<=N; i++){
  97.         for(int j = 1; j<=N; j++){
  98.             if(!vis[i][j]){
  99.                 regions.push_back(dfs(i, j));
  100.             }
  101.         }
  102.     }
  103.    
  104.     int total = 0, used = V;
  105.    
  106.     for(int i = 0; i<regions.size(); i++){
  107.         total += regions[i] * (used - regions[i]);
  108.         used -= regions[i];
  109.     }
  110.    
  111.     cout << total;
  112.        
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement