QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#503384 | #7069. Farm | Decade | WA | 110ms | 20324kb | C++17 | 4.9kb | 2024-08-03 18:16:09 | 2024-08-03 18:16:10 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
// #define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
mt19937 rng;
uniform_int_distribution <ull> dist(0,1ull<<63);
//#define mp make_pair
inline void print(__int128 x){//输出模板
if(x<0){
putchar('-');
x=-x;
}
if(x>9) print(x/10);
putchar(x%10+'0');
}
inline int read()
{
int X = 0;
bool flag = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
flag = 0;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
X = (X << 1) + (X << 3) + ch - '0';
ch = getchar();
}
if (flag)
return X;
return ~(X - 1);
}
const ll mod = 1e9 + 7,INF = 0x3f3f3f3f;
ll qpow(ll a, ll b)
{
ll res = 1;
a %= mod;
assert(b >= 0);
for (; b; b >>= 1)
{
if (b & 1)
res = res * a % mod;
a = a * a % mod;
}
return res;
}
inline int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
const int N = 500010,M = 500010;
struct edge
{
int u,v,w;
bool operator<(const edge x) const
{
return w<x.w;
}
}e[M];
bool cmp(int x,int y)
{
return e[x].w<e[y].w;
}
int vis[N],vis1[N],lfa[N];
int n,m,ans,res,id[N],st[N];
vector<int> p;
PII que[20];
struct mst
{
int fa[N];
void init()
{
for(int i=1;i<=n;i++) fa[i] = i;
}
int find(int x)
{
if(fa[x]!=x) return fa[x] = find(fa[x]);
return fa[x];
}
void merge1(int x,int y,int c)
{
int xx = find(x),yy = find(y);
if(xx!=yy)
{
fa[xx] = yy;
res += c;
}
}
void merge(int x,int y,int c)
{
int xx = find(x),yy = find(y);
if(xx!=yy)
{
fa[xx] = yy;
res += c;
}
}
}fa;
void solve()
{
cin>>n>>m;
fa.init();
for(int i=1;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
e[i] = {a,b,c};
id[i] = i;
}
for(int i=1;i<=n;i++) lfa[i] = i;
int q;
cin>>q;
for(int i=0;i<q;i++)
{
int x,y;
cin>>x>>y;
vis[x] = 1,vis[y] = 1;
que[i] = {x,y};
p.push_back(e[x].u),p.push_back(e[y].v);
fa.merge1(e[x].u,e[x].v,0);
p.push_back(e[y].u),p.push_back(e[y].v);
fa.merge1(e[y].u,e[y].v,0);
}
sort(p.begin(),p.end());
p.erase(unique(p.begin(),p.end()),p.end());
for(auto it:p) vis1[it] = 1;
sort(id+1,id+1+m,cmp);
for(int i=1;i<=m;i++)
{
if(vis[id[i]]) continue;
int u = e[id[i]].u,v = e[id[i]].v,w = e[id[i]].w;
fa.merge(u,v,w);
}
int c = 0,f = 0;
for(int i=1;i<=n;i++)
{
if(fa.fa[i]==i)
{
c++;
}
}
if(c>1)
{
cout<<-1<<'\n';
return;
}
fa.init();
int rest = res;
ans = INF;
for(int i=0;i<(1<<q);i++)
{
res = rest;
vector<edge> vc;
for(auto it:p) fa.fa[it] = it;
for(int j=0;j<q;j++) st[que[j].first] = st[que[j].second] = 0;
for(int j=0;j<q;j++)
{
if((i>>j)&1)
{
if(st[que[j].first]) continue;
vc.push_back(e[que[j].first]),st[que[j].first] = 1;
}
else
{
if(st[que[j].second]) continue;
vc.push_back(e[que[j].second]),st[que[j].second] = 1;
}
}
sort(vc.begin(),vc.end());
for(auto it:vc)
{
int u = it.u,v = it.v,w = it.w;
u = fa.find(u),v = fa.find(v);
if(u!=v)
{
if(vis1[u]) fa.fa[u] = v;
else fa.fa[v] = u;
}
res += w;
}
vc.clear();
for(int j=0;j<q;j++)
{
if(!((i>>j)&1))
{
if(st[que[j].first]) continue;
vc.push_back(e[que[j].first]),st[que[j].first] = 1;
}
else
{
if(st[que[j].second]) continue;
vc.push_back(e[que[j].second]),st[que[j].second] = 1;
}
}
for(auto it:vc)
{
int u = it.u,v = it.v,w = it.w;
u = fa.find(u),v = fa.find(v);
if(u!=v)
{
if(vis1[u]) fa.fa[u] = v;
else fa.fa[v] = u;
res += w;
}
}
ans = min(ans,res);
}
cout<<ans<<'\n';
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
//cin>>t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 15900kb
input:
4 6 1 1 2 2 4 3 1 1 4 2 4 4 3 2 4 1 3 4 1 1 2
output:
11
result:
ok single line: '11'
Test #2:
score: 0
Accepted
time: 107ms
memory: 17024kb
input:
100000 500000 2516 13348 191 37713 25720 216 41568 13765 877 2116 27917 895 76904 65435 37 73053 24687 44 97127 44338 700 2251 85769 378 95166 20208 42 59303 57463 158 26863 18030 31 58613 6818 2 15455 18106 254 3232 13720 610 85677 16778 650 25618 72746 813 80365 162 47 10930 7403 645 79272 54568 6...
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 108ms
memory: 16696kb
input:
100000 500000 34497 87538 658 69862 2776 861 93620 16992 904 77910 81200 149 83935 83752 880 17602 75791 259 85887 53289 710 4200 79358 181 8518 19264 737 94665 47462 822 50632 51994 143 55224 59127 656 615 92858 150 48450 9465 58 35713 45287 140 64861 32248 517 70296 45113 153 11189 90316 809 40673...
output:
12148224
result:
ok single line: '12148224'
Test #4:
score: 0
Accepted
time: 108ms
memory: 20232kb
input:
1 500000 1 1 963 1 1 349 1 1 157 1 1 6 1 1 312 1 1 377 1 1 783 1 1 42 1 1 18 1 1 327 1 1 499 1 1 824 1 1 343 1 1 798 1 1 193 1 1 667 1 1 378 1 1 641 1 1 692 1 1 622 1 1 584 1 1 590 1 1 324 1 1 858 1 1 914 1 1 601 1 1 734 1 1 61 1 1 559 1 1 681 1 1 825 1 1 888 1 1 585 1 1 55 1 1 818 1 1 190 1 1 278 1...
output:
1605
result:
ok single line: '1605'
Test #5:
score: -100
Wrong Answer
time: 110ms
memory: 20324kb
input:
5 500000 5 1 817 2 1 273 3 5 674 1 5 15 5 2 872 3 4 728 3 2 807 5 3 28 2 5 96 1 5 100 4 2 224 4 4 980 5 5 727 2 2 520 4 1 29 2 1 142 4 2 963 4 4 118 4 4 615 4 3 719 5 3 200 5 2 746 4 2 68 5 4 859 1 3 182 3 4 286 3 1 229 4 1 895 2 1 730 1 2 622 2 4 913 2 1 697 5 5 130 4 5 507 5 2 425 2 4 716 2 1 884 ...
output:
3219
result:
wrong answer 1st lines differ - expected: '3097', found: '3219'