QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#800254 | #8253. Very Important Edge | LaVuna47 | TL | 1328ms | 184740kb | C++20 | 4.9kb | 2024-12-06 03:48:05 | 2024-12-06 03:48:06 |
Judging History
answer
/** gnu specific **/
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
/** contains everything I need in std **/
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(S) ((int)S.size())
#define FOR(i, st_, n) for(int i = st_; i < n; ++i)
#define RFOR(i, n, end_) for(int i = (n)-1; i >= end_; --i)
#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<double, double> pdd;
typedef unsigned long long ull;
typedef long double LD;
typedef pair<ull, ull> pull;
using namespace __gnu_pbds;
typedef tree<ll, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
using namespace std;
#ifdef ONPC
mt19937 rnd(228);
#else
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
#endif
namespace std
{
// Define a hash function for std::pair
template<typename T>
inline void hash_combine(std::size_t &seed, const T &v)
{
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T1, typename T2>
struct hash<std::pair<T1, T2>>
{
std::size_t operator()(const std::pair<T1, T2> &key) const
{
std::size_t seed = 0;
hash_combine(seed, key.first);
hash_combine(seed, key.second);
return seed;
}
};
}
struct Edge
{
int u,v;
ll w;
};
struct DSU
{
int n;
vector<int> pr;
DSU(){}
DSU(int nn)
{
n = nn;
pr = vector<int>(n, -1);
for(int i = 0; i < n; ++i)
pr[i] = i;
}
int get_parent(int v)
{
if(pr[v] == v)
return v;
return pr[v] = get_parent(pr[v]);
}
void make_union(int a, int b)
{
a = get_parent(a);
b = get_parent(b);
if(a != b)
{
pr[a] = b;
}
}
};
struct SpecialDSU
{
int n;
vector<int> pr;
vector<int> SZ;
vector<map<ll, set<pii>>> M;
SpecialDSU(){}
SpecialDSU(int nn, const vector<vector<pll>>& adj)
{
n = nn;
pr = vector<int>(n, -1);
SZ = vector<int>(n, 0);
for(int i = 0; i < n; ++i)
pr[i] = i;
M = vector<map<ll, set<pii>>>(n);
FOR(v,0,n)
{
for(auto [to, w]: adj[v])
{
M[v][w].insert(pii{min((int)to,v), max((int)to,v)});
SZ[v] += 1;
}
}
}
ll get_minimum(int v)
{
return M[get_parent(v)].begin()->x;
}
int get_parent(int v)
{
if(pr[v] == v)
return v;
return pr[v] = get_parent(pr[v]);
}
void make_union(int a, int b)
{
a = get_parent(a);
b = get_parent(b);
if(a != b)
{
if(SZ[b] < SZ[a])
swap(a,b);
// sz[b] > sz[a]
pr[a] = b;
SZ[b] += SZ[a];
for(const auto& [w, edge]: M[a])
{
for(const auto& [u,v]: edge)
{
if(get_parent(u) == get_parent(v))
{
M[b][w].erase({min(u,v),max(u,v)});
if(M[b][w].empty())
M[b].erase(w);
}
else
{
M[b][w].insert({min(u,v),max(u,v)});
}
}
}
M[a].clear();
}
}
};
vector<vector<pll>> msp_adj;
vector<vector<pll>> adj;
SpecialDSU dsu;
ll msp;
ll f(int v, int pr, ll ww)
{
ll res = msp;
for(auto [to, w]: msp_adj[v])
{
if(to!=pr)
{
res=max(res,f(to, v, w));
}
}
for(auto [to, w]: msp_adj[v])
{
if(to!=pr)
{
dsu.make_union(v,to);
}
}
if(pr!=-1)
{
res=max(res,msp-ww+dsu.get_minimum(v));
}
return res;
}
int solve()
{
int n,m;
if(!(cin>>n>>m))
return 1;
msp_adj=vector<vector<pll>>(n); // msp tree
adj=vector<vector<pll>>(n); // not msp tree
vector<Edge> edges(m);
for(auto& [u,v,w]: edges)cin>>u>>v>>w, --u,--v;
sort(all(edges),[](const Edge&e1, const Edge&e2) -> bool {
return e1.w<e2.w;
});
auto dsu_ = DSU(n);
msp=0;
for(auto [u,v,w]:edges)
{
if(dsu_.get_parent(u) == dsu_.get_parent(v))
{
adj[u].pb({v,w});
adj[v].pb({u,w});
continue;
}
msp += w;
dsu_.make_union(u,v);
msp_adj[u].pb({v,w});
msp_adj[v].pb({u,w});
}
dsu = SpecialDSU(n,adj);
cout << f(0,-1,0)<<'\n';
return 0;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int TET = 1e9;
//cin >> TET;
for (int i = 1; i <= TET; i++)
{
if (solve())
{
break;
}
#ifdef ONPC
cout << "__________________________" << endl;
#endif
}
#ifdef ONPC
cerr << endl << "finished in " << clock() * 1.0 / CLOCKS_PER_SEC << " sec" << endl;
#endif
}
详细
Test #1:
score: 100
Accepted
time: 965ms
memory: 170192kb
input:
1000 499500 26 715 732723 850 918 507404 276 643 372190 67 702 972051 397 777 337003 178 185 863117 61 151 943008 83 581 731622 99 501 3260 301 588 948638 17 908 147104 193 480 94512 347 415 416562 519 912 627431 70 959 86892 333 573 757685 129 197 181261 224 636 259176 335 426 567962 193 339 70097 ...
output:
1121154
result:
ok single line: '1121154'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 3 1 2 1 2 3 2 1 3 2
output:
4
result:
ok single line: '4'
Test #3:
score: 0
Accepted
time: 0ms
memory: 3660kb
input:
4 5 2 3 5 1 2 2 1 3 4 1 4 2 3 4 3
output:
10
result:
ok single line: '10'
Test #4:
score: 0
Accepted
time: 0ms
memory: 3860kb
input:
5 7 2 5 8 1 3 19 4 5 9 1 5 15 1 2 14 3 4 16 2 4 15
output:
54
result:
ok single line: '54'
Test #5:
score: 0
Accepted
time: 1014ms
memory: 169856kb
input:
1000 499500 857 965 96030 394 688 78612 440 754 495692 48 297 76650 206 975 200873 790 854 325696 307 384 472048 94 958 278751 601 806 136479 880 988 278407 265 813 315222 114 470 208185 172 332 425333 504 669 12025 266 315 400789 61 894 120668 675 972 228141 604 855 494399 116 496 234186 116 888 21...
output:
627123
result:
ok single line: '627123'
Test #6:
score: 0
Accepted
time: 14ms
memory: 6612kb
input:
1000 10000 894 939 776049 780 873 504700 126 161 227849 221 391 589404 562 661 697593 8 495 975484 13 527 481938 711 908 92209 147 616 682518 117 849 292092 231 463 868315 296 372 308904 458 886 970257 44 415 858179 2 352 402538 340 431 276296 87 744 48795 30 146 526326 35 109 788908 551 742 146887 ...
output:
64112840
result:
ok single line: '64112840'
Test #7:
score: 0
Accepted
time: 1328ms
memory: 184740kb
input:
100000 1000000 3496 42038 2 23760 54893 2 40179 73909 2 18246 58964 2 59829 97488 2 46535 89743 2 43598 88684 2 10025 15117 2 25372 39316 2 55988 99623 2 12242 94927 2 91339 99004 2 68898 82761 2 19117 49957 2 24435 85140 2 15302 78102 2 9038 89450 2 82914 88120 2 6227 67500 2 5298 26787 2 27614 518...
output:
100000
result:
ok single line: '100000'
Test #8:
score: 0
Accepted
time: 71ms
memory: 41764kb
input:
100000 150000 10397 97917 1 81023 97767 2 27616 48830 2 17714 90000 2 34151 34285 1 6030 96304 1 50786 80688 1 30193 63737 1 25856 97783 1 735 46702 2 24633 79153 1 27172 85261 1 18963 27646 1 68548 74906 1 45452 65265 2 20050 96249 1 42929 94752 1 30314 52715 2 17457 32389 1 79882 95851 1 20932 436...
output:
100000
result:
ok single line: '100000'
Test #9:
score: 0
Accepted
time: 276ms
memory: 38952kb
input:
1000 249502 53 877 1 475 560 1 559 895 1 672 838 1 68 950 1 105 805 1 636 879 1 597 991 1 601 738 1 26 194 1 169 920 1 47 787 1 470 882 1 253 734 1 454 803 1 302 998 1 523 678 1 191 415 1 279 687 1 261 595 1 373 780 1 28 977 1 393 412 1 315 676 1 6 474 1 142 246 1 164 413 1 548 960 1 316 849 1 157 8...
output:
1000
result:
ok single line: '1000'
Test #10:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
6 8 1 2 1 4 5 2 1 3 3 4 6 4 2 3 5 5 6 6 1 4 7 2 5 1000
output:
1010
result:
ok single line: '1010'
Test #11:
score: -100
Time Limit Exceeded
input:
100000 1000000 12367 40267 217042 44689 75139 285000 27281 61254 650783 64350 65848 90802 31547 46704 668855 30690 87250 123830 55229 89914 134555 43197 77248 447563 59620 64482 84718 67546 90659 107138 87307 99160 861726 36383 55308 767446 1561 33387 137971 24268 29109 347562 15682 32464 829425 175...