Advertisement
i_love_rao_khushboo

Problem by Muskan

Jun 12th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.13 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define ld long double
  6. #define ull unsigned long long
  7. #define pb push_back
  8. #define ppb pop_back
  9. #define mp make_pair
  10. #define F first
  11. #define S second
  12. #define PI 3.1415926535897932384626
  13. #define sz(x) ((int)(x).size())
  14.  
  15. typedef pair<int, int> pii;
  16. typedef pair<ll, ll> pll;
  17. typedef vector<int> vi;
  18. typedef vector<ll> vll;
  19. typedef vector<ull> vull;
  20. typedef vector<bool> vb;
  21. typedef vector<char> vc;
  22. typedef vector<string> vs;
  23. typedef vector<pii> vpii;
  24. typedef vector<pll> vpll;
  25. typedef vector<vi> vvi;
  26. typedef vector<vll> vvll;
  27. typedef vector<vull> vvull;
  28. typedef vector<vb> vvb;
  29. typedef vector<vc> vvc;
  30. typedef vector<vs> vvs;
  31.  
  32. /************************************************** DEBUGGER *******************************************************************************************************/
  33.  
  34. #ifndef ONLINE_JUDGE
  35. #define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
  36. #else
  37. #define debug(x)
  38. #endif
  39.  
  40. void _print(ll t) { cerr << t; }
  41. void _print(int t) { cerr << t; }
  42. void _print(string t) { cerr << t; }
  43. void _print(char t) { cerr << t; }
  44. void _print(ld t) { cerr << t; }
  45. void _print(double t) { cerr << t; }
  46. void _print(ull t) { cerr << t; }
  47.  
  48. template <class T, class V> void _print(pair <T, V> p);
  49. template <class T> void _print(vector <T> v);
  50. template <class T> void _print(vector <vector<T>> v);
  51. template <class T> void _print(set <T> v);
  52. template <class T, class V> void _print(map <T, V> v);
  53. template <class T> void _print(multiset <T> v);
  54. template <class T, class V> void _print(pair <T, V> p) { cerr << "{"; _print(p.F); cerr << ","; _print(p.S); cerr << "}"; }
  55. template <class T> void _print(vector <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  56. template <class T> void _print(vector <vector<T>> v) { cerr << "==>" << endl; for (vector<T> vec : v) { for(T i : vec) {_print(i); cerr << " "; } cerr << endl; } }
  57. template <class T> void _print(set <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  58. template <class T> void _print(multiset <T> v) { cerr << "[ "; for (T i : v) {_print(i); cerr << " "; } cerr << "]"; }
  59. template <class T, class V> void _print(map <T, V> v) { cerr << "[ "; for (auto i : v) {_print(i); cerr << " "; } cerr << "]"; }
  60.  
  61. /*******************************************************************************************************************************************************************/
  62.  
  63. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  64. int rng(int lim) {
  65.     uniform_int_distribution<int> uid(0,lim-1);
  66.     return uid(rang);
  67. }
  68.  
  69. const int INF = 0x3f3f3f3f;
  70. const int mod = 1e9+7;
  71.  
  72. ll mod_exp(ll a, ll b) { a %= mod; if(a == 0) return 0LL; ll res = 1LL;
  73.                          while(b > 0) { if(b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }
  74.                          
  75. ll mod_inv(ll a) { return mod_exp(a, mod - 2); } // works only for prime value of "mod"
  76. ll GCD(ll a, ll b) { return (b == 0) ? a : GCD(b, a % b); }
  77.  
  78. /******************************************************************************************************************************/
  79.  
  80. vvi g;
  81.  
  82. // to store all articulation points
  83. vector<int> point;
  84.  
  85. vi wt;
  86. vb vis;
  87. int n, m;
  88.  
  89. void dfs(int curr, int par, vector<int> &entry, vector<int> &low, int time)
  90. {
  91.     entry[curr]=low[curr]=time++;
  92.    
  93.     // for storing the count of #child of the curr node
  94.     // in the DFS Tree (not in the graph)
  95.     int no_of_child=0;
  96.    
  97.     // now, there are 2 cases
  98.     // 1. if the child is not visited, then visit it
  99.     //    and ask for the lowest time of that child
  100.     // 2. if the child is visited and != parent of curr,
  101.     //    then backedge is present and therefore cycle is there
  102.     for(auto child: g[curr])
  103.     {
  104.         // case 1
  105.         if(entry[child]==0)
  106.         {
  107.             dfs(child, curr, entry, low, time);
  108.             // increment the #child of curr
  109.             no_of_child++;
  110.            
  111.             low[curr]=min(low[curr], low[child]);
  112.            
  113.             // condition for curr to be an articulation pt
  114.             if(par!=-1 && low[child]>=entry[curr])
  115.                 point.push_back(curr);
  116.         }
  117.        
  118.         // case 2
  119.         else if(child != par)
  120.             low[curr]=min(low[curr], entry[child]);
  121.     }
  122.    
  123.     // separate case for the root of the DFS TREE of the graph
  124.     // to be an articulation pt
  125.     if(par==-1 && no_of_child>=2)
  126.     {
  127.         // the parent of the root of DFS tree was passed as 1
  128.         point.push_back(curr);
  129.     }
  130.    
  131.     return;
  132. }
  133.  
  134. void artpts_and_bridges()
  135. {  
  136.     // to store the entry/discovery time of all vertices
  137.     // while DFS traversal, it will also be used as visited array
  138.     vector<int> entry(n, 0);
  139.  
  140.     // to store the lowest time of all the vertices
  141.     vector<int> low(n, 0);
  142.    
  143.     // initialising the time as 1
  144.     int time=1;
  145.    
  146.     for(int i = 0; i < n; i++)
  147.     {
  148.         // if not visited yet, call dfs by taking
  149.         // i as the root of the current DFS traversal
  150.         if(entry[i]==0)
  151.             dfs(i, -1, entry, low, time);
  152.     }
  153. }
  154.  
  155. void dfs_(int cur, ll &x, int ap) {
  156.     vis[cur] = 1;
  157.     x += wt[cur];
  158.    
  159.     for(auto child: g[cur]) {
  160.         if(!vis[child] and child != ap) dfs_(child, x, ap);
  161.     }
  162. }
  163.  
  164. void solve()
  165. {
  166.     cin >> n >> m;
  167.    
  168.     g.clear(); g.resize(n);
  169.     point.clear();
  170.     wt.clear(); wt.resize(n);
  171.     vis.clear(); vis.resize(n, 0);
  172.    
  173.     for(int i = 0; i < n; i++) cin >> wt[i];
  174.    
  175.     for(int i = 0; i < m; i++) {
  176.         int x, y; cin >> x >> y;
  177.         x -= 1, y -= 1;
  178.         g[x].pb(y); g[y].pb(x);
  179.     }
  180.    
  181.     artpts_and_bridges();  
  182.    
  183.     ll sum = accumulate(wt.begin(), wt.end(), 0LL);
  184.    
  185.     if(sz(point) == 0) {
  186.         int mx = *max_element(wt.begin(), wt.end());
  187.         cout << 0 << " " << sum - mx << "\n";
  188.     }
  189.    
  190.     else {
  191.         vpii tmp;
  192.         for(int i = 0; i < sz(point); i++) tmp.pb({wt[point[i]], point[i]});
  193.         sort(tmp.begin(), tmp.end());
  194.        
  195.         vis[tmp[0].S] = 1;
  196.  
  197.         vll comp1;
  198.         for(int i = 0; i < n; i++) {
  199.             if(!vis[i] and i != tmp[0].S) {
  200.                 ll x = 0LL;
  201.                 dfs_(i, x, tmp[0].S);
  202.                 comp1.pb(x);
  203.             }
  204.         }
  205.        
  206.         sort(comp1.rbegin(), comp1.rend());
  207.        
  208.         if(sz(tmp) > 1) {
  209.             for(int i = 0; i < n; i++) vis[i] = 0;
  210.             vis[tmp[1].S] = 1;
  211.            
  212.             vll comp2;
  213.             for(int i = 0; i < n; i++) {
  214.                 if(!vis[i] and i != tmp[1].S) {
  215.                     ll x = 0LL;
  216.                     dfs_(i, x, tmp[1].S);
  217.                     comp2.pb(x);
  218.                 }
  219.             }
  220.            
  221.             sort(comp2.rbegin(), comp2.rend());
  222.            
  223.             if(comp1[1] > comp2[1]) cout << comp1[1] << " " << comp1[0] << "\n";
  224.             else if(comp1[1] < comp2[1]) cout << comp2[1] << " " << comp2[0] << "\n";
  225.             else {
  226.                 cout << comp1[1] << " ";
  227.                 if(comp1[0] < comp2[0]) cout << comp1[0] << "\n";
  228.                 else cout << comp2[0] << "\n";
  229.             }
  230.         }
  231.        
  232.         else cout << comp1[1] << " " << comp1[0] << "\n";
  233.     }
  234. }
  235.  
  236. int main()
  237. {
  238.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  239.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  240.  
  241.     // #ifndef ONLINE_JUDGE
  242.     //     freopen("input.txt", "r", stdin);
  243.     //     freopen("output.txt", "w", stdout);
  244.     // #endif
  245.    
  246.     // #ifndef ONLINE_JUDGE
  247.     //      freopen("error.txt", "w", stderr);
  248.     // #endif
  249.    
  250.     int t = 1;
  251.     // int test = 1;
  252.     cin >> t;
  253.     while(t--) {
  254.         // cout << "Case #" << test++ << ": ";
  255.         solve();
  256.     }
  257.  
  258.     return 0;
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement