QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#503375 | #7069. Farm | Decade | RE | 1ms | 5592kb | C++17 | 4.4kb | 2024-08-03 18:09:00 | 2024-08-03 18:09:00 |
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 = 100010,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];
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++)
{
if((i>>j)&1)vc.push_back(e[que[j].first]);
else vc.push_back(e[que[j].second]);
}
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))vc.push_back(e[que[j].first]);
else vc.push_back(e[que[j].second]);
}
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;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 5592kb
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: -100
Runtime Error
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...