QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#387840#7871. 命运valeriu#0 1ms3568kbC++204.0kb2024-04-12 21:44:342024-07-04 03:36:20

Judging History

This is the latest submission verdict.

  • [2024-07-04 03:36:20]
  • Judged
  • Verdict: 0
  • Time: 1ms
  • Memory: 3568kb
  • [2024-04-12 21:44:34]
  • Submitted

answer

#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

using ll = long long;
using ld = long double;

#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

const int nmax = 5e4 + 5, inf = 1e9 + 6;

vector<pii> g[nmax];
int color[nmax];

void fill(int node, int C) {
   if(color[node] != 0) return;
   color[node] = C;
   //cerr << node << ' ' << C << '\n';
   for(auto [x, w] : g[node]) fill(x, C);
}

namespace DSU {
   int dsu[nmax], links[nmax];
   int cc;
   ll stdcost, supra;
   
   vector<tii> stundo;
   
   void init(int n) {
      cc = n;
      stdcost = 0;
      for(int i = 1; i <= n; i++) dsu[i] = -1, links[i] = 0;
      return;
   }
   int f(int x) { return dsu[x] < 0? x : f(dsu[x]); }
   bool unite(int x, int y) {
      //auto [a, b] = make_pair(x, y);
      x = f(x);
      y = f(y);
      //cerr << x << ' ' << y << '\n';
      if(x == y) {
         stundo.emplace_back(x, y, 0);
         return 0;
      }
      
      if(-dsu[x] < -dsu[y]) swap(x, y);
      
      stundo.emplace_back(x, y, dsu[y]);
      dsu[x] += dsu[y];
      dsu[y] = x;
      links[x] += links[y];
      cc--;
      
      return 1;
   }
   
   void addlink(int node, int aggr) {
      links[node] += aggr;
      if(dsu[node] < 0) return;
      addlink(dsu[node], aggr);
   }
   
   bool undo() {
      auto [x, y, A] = stundo.back();
      stundo.pop_back();
      if(x == y) return 0;
      dsu[y] = A;
      dsu[x] -= A;
      links[x] -= links[y];
      cc++;
      return 1;
   }
}

signed main() {
   cin.tie(0) -> sync_with_stdio(0);
   int n, m, k, s;
   cin >> n >> m >> k >> s;
   
   vector<tii> edgs;
   
   for(int i = 0, x, y, w; i < m; i++) {
      cin >> x >> y >> w;
      if(y == s) swap(x, y);
      else if(x != s && x > y) swap(x, y);
      edgs.emplace_back(x, y, w);
   }
   
   pii prv = {-1, -1};
   sort(all(edgs));
   
   vector<tii> nvedgs;
   
   for(auto [x, y, w] : edgs) {
      if(prv == make_pair(x, y) && x == s) continue;
      g[x].emplace_back(y, w);
      g[y].emplace_back(x, w);
      prv = pii{x, y};
      nvedgs.emplace_back(x, y, w);
   }
   
   edgs = move(nvedgs);
   
   {
      color[s] = -1;
      int C = 1;
      for(int i = 1; i <= n; i++) {
         if(color[i] == 0) fill(i, C++);
      }
      C--;
      
      if(sz(g[s]) < k) {
         cout << "nonnondayo\n";
         return 0;
      }
      if(!([&]() {
         vector<int> found_colors(C, 0);
         for(auto [x, w] : g[s]) found_colors[color[x] - 1] = 1;
         if((accumulate(all(found_colors), 0) != C) || C > k) return false;
         return true;
      }())) {
         cout << "nonnondayo\n";
         return 0;
      }
   }
   
   DSU::init(n);
   DSU::cc--;
   
   sort(all(edgs), [&](auto a, auto b) { return get<2>(a) < get<2>(b); });
   
   for(auto [x, w] : g[s]) {
      DSU::addlink(x, 1);
   }
   
   vector<tii> keeper;
   for(auto [a, b, w] : edgs) {
      if(a == s) continue;
      DSU::unite(a, b);
   }
   
   for(int i = sz(edgs) - 1; i >= 0; i--) {
      auto [a, b, w] = edgs[i];
      //cerr << a << ' ' << b << '\t' << DSU::cc << '\n';
      if(a != s) {
         DSU::undo();
         if(DSU::cc > k) {
            keeper.emplace_back(edgs[i]);
            while(sz(DSU::stundo)) DSU::undo();
            for(auto [u, v, T] : keeper) if(u != s) DSU::unite(u, v);
            for(auto [u, v, T] : edgs | views::take(i)) if(u != s) DSU::unite(u, v);
         }
      }
      else {
         if(DSU::links[DSU::f(b)] == 1)
            keeper.emplace_back(edgs[i]),
            k--;
         else
            DSU::addlink(b, -1);
      }
   }
   
   ll total = 0;
   for(auto [a, b, w] : keeper) {
      total += w;
      //cout << a << ' ' << b << '\n';
   }
   
   cout << total << '\n';
}

/**
      Istenem! Nu poate fi real.
-- Surse verificate
*/


詳細信息

Subtask #1:

score: 0
Time Limit Exceeded

Test #1:

score: 0
Time Limit Exceeded

input:

49277 49276 1 49277
1 2 2000
2 3 3000
2 4 4000
3 5 5000
3 6 6000
4 7 7000
1 8 8000
7 9 9000
1 10 10000
5 11 11000
4 12 12000
3 13 13000
13 14 14000
1 15 15000
7 16 16000
11 17 17000
2 18 18000
10 19 19000
6 20 20000
4 21 21000
17 22 22000
1 23 23000
14 24 24000
4 25 25000
16 26 26000
15 27 27000
9 2...

output:


result:


Subtask #2:

score: 0
Wrong Answer

Test #5:

score: 10
Accepted
time: 1ms
memory: 3568kb

input:

21 20 2 21
1 2 18581
1 3 9620
2 4 4362
1 5 7702
5 6 17435
4 7 11679
3 8 14832
1 9 15073
2 10 6595
5 11 19676
11 12 16943
12 13 5268
5 14 20262
8 15 8192
7 16 12340
7 17 1437
7 18 3064
2 19 10437
12 20 2882
19 21 13483

output:

nonnondayo

result:

ok single line: 'nonnondayo'

Test #6:

score: -10
Wrong Answer
time: 1ms
memory: 3548kb

input:

10 20 4 3
1 2 18247
2 3 9041
2 4 4031
2 5 7363
3 6 17172
1 7 11014
6 8 14781
8 9 15347
8 10 6598
5 9 19331
3 10 16523
10 6 5732
2 3 20357
6 9 8827
10 3 12864
6 3 1551
5 6 3932
3 1 10223
9 3 2296
8 5 13620

output:

48257

result:

wrong answer 1st lines differ - expected: '54418', found: '48257'

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Skipped

Dependency #1:

0%

Subtask #5:

score: 0
Time Limit Exceeded

Test #21:

score: 0
Time Limit Exceeded

input:

9992 99999 500 1
1 2 96661121
1 3 21915940
1 4 110026320
1 5 37433129
1 6 38560320
1 7 83209024
1 8 80352054
1 9 88957672
1 10 65449874
1 11 38239186
1 12 90153728
1 13 30810974
1 14 96493404
1 15 112886259
1 16 87190053
1 17 91182163
1 18 107303768
1 19 101439823
1 20 120601875
1 21 122197599
1 22 ...

output:


result:


Subtask #6:

score: 0
Skipped

Dependency #3:

0%

Subtask #7:

score: 0
Skipped

Dependency #1:

0%