Advertisement
willy108

Metal Processing Plant

Jul 25th, 2022
908
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.80 KB | None | 0 0
  1.  
  2. //misaka and elaina will carry me to master
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <utility>
  8. #include <cassert>
  9. #include <algorithm>
  10. #include <vector>
  11. #include <functional>
  12. #include <numeric>
  13. #include <set>
  14. #include <array>
  15. #include <queue>
  16. #include <map>
  17. #include <chrono>
  18. #include <random>
  19.  
  20. #define ll long long
  21. #define lb long double
  22. #define sz(vec) ((int)(vec.size()))
  23. #define all(x) x.begin(), x.end()
  24. #define pb push_back
  25. #define mp make_pair
  26. #define kill(x, s) {int COND = x; if(COND){ cout << s << "\n"; return ; }}
  27.  
  28. #ifdef ONLINE_JUDGE
  29. #define cerr while(0) cerr
  30. #endif
  31.  
  32. const lb eps = 1e-9;
  33. const ll mod = 1e9 + 7, ll_max = 1e18;
  34. //const ll mod = (1 << (23)) * 119 +1, ll_max = 1e18;
  35. const int MX = 400 +10, int_max = 0x3f3f3f3f;
  36.  
  37. struct {
  38.   template<class T>
  39.   operator T() {
  40.     T x; std::cin >> x; return x;
  41.   }
  42. } in;
  43.  
  44. using namespace std;
  45.  
  46. vector<int> rel;
  47.  
  48. vector<int> adj[MX], adj2[MX], tr[MX];
  49. vector<int> scc[MX];
  50. vector<array<int, 3>> edges;
  51. int n;
  52. int id[MX];
  53. int vis[MX];
  54. vector<int> comp, order;
  55. int tim = 0;
  56.  
  57. #define LC(k) (k)
  58. #define RC(k) (k + n)
  59.  
  60. void add_or(int x, int y){
  61.     adj[RC(y)].pb(LC(x));
  62.     adj[RC(x)].pb(LC(y));
  63.     adj2[LC(y)].pb(RC(x));
  64.     adj2[LC(x)].pb(RC(y));
  65. }
  66.  
  67. void add_xor(int x, int y){
  68.     adj[LC(x)].pb(RC(y));  
  69.     adj[RC(x)].pb(LC(y));
  70.     adj[LC(y)].pb(RC(x));
  71.     adj[RC(y)].pb(LC(x));
  72.     adj2[LC(x)].pb(RC(y)); 
  73.     adj2[RC(x)].pb(LC(y));
  74.     adj2[LC(y)].pb(RC(x));
  75.     adj2[RC(y)].pb(LC(x));
  76. }
  77.  
  78. void dfs1(int u){
  79.     vis[u] = 1;
  80.     for(int v : adj[u]){
  81.         if(!vis[v]){
  82.             dfs1(v);
  83.         }
  84.     }
  85.     order.pb(u);
  86. }
  87.  
  88. void dfs2(int u, int s){
  89.     vis[u] = 1;
  90.     scc[s].pb(u);
  91.     comp.pb(u);
  92.     for(int v : adj2[u]){
  93.         if(!vis[v]){
  94.             dfs2(v, s);
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100. int check(int x, int y){
  101.     for(int i = 0; i<=n*2+4; i++){
  102.         adj[i].clear();
  103.         adj2[i].clear();
  104.         scc[i].clear();
  105.         id[i] = 0;
  106.     }
  107.     order.clear();
  108.     comp.clear();
  109.     memset(vis, 0, sizeof(vis));
  110.     for(auto [w, u, v] : edges){
  111.         if(w > max(x, y)){
  112.             add_xor(u, v);
  113.         }else if(w > min(x, y)){
  114.             add_or(u, v);
  115.         }  
  116.     }
  117.     for(int i = 1; i<=n*2; i++){
  118.         if(!vis[i]){
  119.             dfs1(i);
  120.         }
  121.     }
  122.     reverse(all(order));
  123.     memset(vis, 0, sizeof(vis));
  124.     for(int x : order){
  125.         if(!vis[x]){
  126.             dfs2(x, x);
  127.         }
  128.     }
  129.     for(int i = 1; i<=n*2; i++){
  130.         for(int v : scc[i]){
  131.             id[v] = i;
  132.         }
  133.     }
  134.     for(int i = 1; i<=n; i++){
  135.         if(id[i] == id[i + n]) return 0;
  136.     }  
  137.     return 1;  
  138. }
  139.  
  140. int binsearch(int x){
  141.     int l = -1, r = 1e9 +10;
  142.     while(l + 1 != r){
  143.         int mid = (l + r)/2;
  144.         if(!check(x, mid)){
  145.             l = mid;
  146.         }else{
  147.             r = mid;
  148.         }
  149.     }
  150.     return l + x + 1;
  151. }
  152.  
  153. int yy[MX], cc[MX];
  154.  
  155. int find(int x){
  156.     if(x == yy[x]) return x;
  157.     return yy[x] = find(yy[x]);
  158. }
  159.  
  160. void Union(int a, int b){
  161.     a = find(a), b = find(b);
  162.     yy[a] = b;
  163. }
  164.  
  165. void color(int u, int p){
  166.     cc[u] = cc[p]^1;
  167.     for(int v : tr[u]){
  168.         if(v != p) color(v, u);
  169.     }
  170. }
  171.  
  172. int last_case(){
  173.     color(1, 0);
  174.     array<int, 2> D = {0, 0};
  175.     for(auto [w, u, v] : edges){
  176.         if(cc[u] == cc[v]){
  177.             D[cc[u]] = max(D[cc[u]], w);
  178.         }
  179.     }
  180.     rel.pb(max(D[1], D[0]));
  181.     return D[0] + D[1];
  182. }
  183.  
  184. void solve(){
  185.     n = in;
  186.     kill(n <= 2, 0);
  187.     for(int i = 1; i<=n-1; i++){
  188.         for(int j = i+1; j<=n; j++){
  189.             int a = in;
  190.             edges.pb({a, i, j});
  191.         }
  192.     }
  193.     sort(all(edges));
  194.     reverse(all(edges));
  195.         iota(yy+1, yy+1+n, 1);
  196.     for(auto [w, u, v] : edges){
  197.         //rel.pb(w);
  198.         if(find(u) != find(v)){
  199.             rel.pb(w);
  200.             Union(u, v);
  201.             tr[u].pb(v);
  202.             tr[v].pb(u);
  203.         }
  204.     }
  205.     int ans = int_max*2;
  206.     last_case();
  207.     sort(all(rel));
  208.     rel.resize(unique(all(rel)) -rel.begin());
  209.     for(int x : rel){
  210.         if(x >= ans) continue ;
  211.         ans = min(ans, binsearch(x));
  212.     }
  213.     cout << ans << "\n";
  214. }
  215.  
  216. signed main(){
  217.   cin.tie(0) -> sync_with_stdio(0);
  218.  
  219.   int T = 1;
  220.   //cin >> T;
  221.   for(int i = 1; i<=T; i++){
  222.         solve();
  223.     }
  224.   return 0;
  225. }
  226.  
  227.  
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement