Submission #261397

#TimeUsernameProblemLanguageResultExecution timeMemory
261397patrikpavic2Designated Cities (JOI19_designated_cities)C++17
7 / 100
698 ms39440 KiB
#include <cstdio> #include <cstring> #include <cstdlib> #include <vector> #include <set> #define X first #define Y second #define PB push_back using namespace std; typedef long long ll; typedef pair < int, int > pii; typedef pair < int, pii > pip; const int N = 2e5 + 500; vector < pii > v[N]; vector < pip > v2[N]; set < pii > S; int n, deg[N], bio[N]; ll cur, bst[N]; ll C[N], uk; void start(int x, int lst){ for(pip nxt : v2[x]){ if(nxt.X == lst) continue; C[1] += nxt.Y.X; start(nxt.X, x); } } void dfs(int x, int lst){ for(pip nxt : v2[x]){ if(nxt.X == lst) continue; C[nxt.X] = C[x] - nxt.Y.X + nxt.Y.Y; dfs(nxt.X, x); } bst[1] = max(bst[1], C[x]); } inline int pronadi(int x){ for(pii nxt : v[x]) if(deg[nxt.X]) return nxt.Y; return 0; } int main(){ for(int i = 0;i < N;i++) bst[i] = 1e18; scanf("%d", &n); for(int i = 1;i < n;i++){ int x, y, a, b; scanf("%d%d%d%d", &x, &y, &b, &a); v[x].PB({y, a}); v[y].PB({x, b}); v2[x].PB({y, {a, b}}); v2[y].PB({x, {b, a}}); uk += a + b; deg[x]++; deg[y]++; } for(int i = 1;i <= n;i++){ if(deg[i] == 1) S.insert({v[i][0].Y, i}); } bst[(int)S.size()] = cur; int lisce = (int)S.size(); for(;S.size() > 2;){ int brs = S.begin() -> Y; cur += S.begin() -> X; deg[brs] = 0; S.erase(S.begin()); for(pii nxt : v[brs]){ if(deg[nxt.X] > 0){ deg[nxt.X]--; if(deg[nxt.X] == 1) S.insert({pronadi(nxt.X), nxt.X}); break; } } bst[(int)S.size()] = min(bst[(int)S.size()], cur); } bst[1] = 0; start(1, 1); dfs(1, 1); bst[1] = uk - bst[1]; int q; scanf("%d", &q); for(;q--;){ int x; scanf("%d", &x); x = min(x, lisce); printf("%lld\n", bst[x]); } }

Compilation message (stderr)

designated_cities.cpp: In function 'int main()':
designated_cities.cpp:55:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
designated_cities.cpp:57:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x, y, a, b; scanf("%d%d%d%d", &x, &y, &b, &a);
                   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
designated_cities.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int q; scanf("%d", &q);
         ~~~~~^~~~~~~~~~
designated_cities.cpp:88:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int x; scanf("%d", &x);
          ~~~~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...