Advertisement
GerONSo

E 15

Jan 11th, 2020
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. /*
  2.  
  3. ∧_∧
  4. ( ・ω・。)つ━☆・*。
  5. ⊂  ノ    ・゜
  6. しーJ   Accepted
  7.  
  8. */
  9.  
  10.  
  11.  
  12. // #pragma GCC optimize("O3")
  13. // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  14.  
  15. #include <bits/stdc++.h>
  16. #include <ext/pb_ds/assoc_container.hpp>
  17. #include <ext/pb_ds/tree_policy.hpp>
  18.  
  19. #define ll long long
  20. #define all(x) begin(x), end(x)
  21. #define x first
  22. #define y second
  23. #define int long long
  24.  
  25. using namespace std;
  26. using namespace __gnu_pbds;
  27.  
  28. typedef long double ld;
  29. template<typename T>
  30. using kawaii_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  31.  
  32. const ld PI = atan2(0, -1);
  33.  
  34. void seriy() {
  35. ios::sync_with_stdio(0);
  36. cin.tie(0);
  37. cout.tie(0);
  38. cout << fixed << setprecision(14);
  39. #ifdef _offline
  40. freopen("input.txt", "r", stdin);
  41. freopen("output.txt", "w", stdout);
  42. #endif
  43. }
  44.  
  45. const int MAXN = 1e6 + 10;
  46. const int MAXM = 600;
  47. const int INF = 1e18 + 7;
  48. const int BASE = 47;
  49. const int MOD = 1e9 + 7;
  50. const int MAXLOG = 51;
  51. const ld EPS = 1e-6;
  52.  
  53. struct edge {
  54. int u, v, val;
  55. };
  56.  
  57. vector<int> used, p, sz;
  58. bool f = 0;
  59.  
  60. void dfs(vector<vector<int>> &g, int u, int p) {
  61. used[u] = 1;
  62. for(auto v : g[u]) {
  63. if(!used[v]) {
  64. dfs(g, v, u);
  65. }
  66. else if(v != p) {
  67. f = 1;
  68. }
  69. }
  70. }
  71.  
  72. int get(int a) {
  73. if(p[a] == a) return a;
  74. return p[a] = p[p[a]];
  75. }
  76.  
  77. void unite(int a, int b) {
  78. a = get(a);
  79. b = get(b);
  80. if(a != b) {
  81. if(sz[b] > sz[a]) {
  82. swap(a, b);
  83. }
  84. p[b] = a;
  85. sz[a] += sz[b];
  86. }
  87. }
  88.  
  89. signed main() {
  90. seriy();
  91. int n, m;
  92. cin >> n >> m;
  93. used.resize(n);
  94. vector<pair<int, int>> a(m), b(m);
  95. vector<int> w(m);
  96. for(int i = 0; i < m; i++) {
  97. cin >> a[i].x >> a[i].y >> b[i].x >> b[i].y >> w[i];
  98. a[i].x--;
  99. a[i].y--;
  100. b[i].x--;
  101. b[i].y--;
  102. }
  103. if(m == n - 1) {
  104. vector<edge> es(m);
  105. for(int i = 0; i < m; i++) {
  106. es[i] = {b[i].x, b[i].y, w[i]};
  107. }
  108. sort(all(es), [&](edge a, edge b) {
  109. return a.val > b.val;
  110. });
  111. p.resize(n);
  112. sz.resize(n, 1);
  113. for(int i = 0; i < n; i++) {
  114. p[i] = i;
  115. }
  116. int cnt = 0, ans = 0;
  117. for(auto e : es) {
  118. if(get(e.u) != get(e.v)) {
  119. unite(e.u, e.v);
  120. ans += e.val;
  121. cout << ans << '\n';
  122. cnt++;
  123. }
  124. }
  125. while(cnt < m) {
  126. cout << "Impossible\n";
  127. cnt++;
  128. }
  129. return 0;
  130. }
  131. for(int i = 1; i <= m; i++) {
  132. int ans = -INF;
  133. for(int j = 0; j < (1 << m); j++) {
  134. if(__builtin_popcount(j) != i) continue;
  135. vector<vector<int>> g(n), r(n);
  136. int sum = 0;
  137. for(int k = 0; k < m; k++) {
  138. if((j >> k) & 1) {
  139. g[a[k].x].push_back(a[k].y);
  140. g[a[k].y].push_back(a[k].x);
  141. r[b[k].x].push_back(b[k].y);
  142. r[b[k].y].push_back(b[k].x);
  143. sum += w[k];
  144. }
  145. }
  146. f = 0;
  147. fill(all(used), 0);
  148. for(int k = 0; k < n; k++) {
  149. if(!used[k]) {
  150. dfs(g, k, -1);
  151. }
  152. }
  153. if(f) {
  154. continue;
  155. }
  156. f = 0;
  157. fill(all(used), 0);
  158. for(int k = 0; k < n; k++) {
  159. if(!used[k]) {
  160. dfs(r, k, -1);
  161. }
  162. }
  163. if(f) {
  164. continue;
  165. }
  166. // if(i == 3) {
  167. // cerr << j << '\n';
  168. // }
  169. ans = max(ans, sum);
  170. }
  171. if(ans == -INF) {
  172. cout << "Impossible\n";
  173. }
  174. else {
  175. cout << ans << '\n';
  176. }
  177. }
  178. return 0;
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement