QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#503404 | #7069. Farm | lllllu5454 | WA | 101ms | 29900kb | C++20 | 3.8kb | 2024-08-03 18:27:31 | 2024-08-03 18:27:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100, mod = 998244353;
class edge
{
public:
int x, y, w;
bool operator<(const edge x) const
{
return w < x.w;
}
} e[N];
vector<edge> v1, v2;
int f[N];
struct DSU
{
std::vector<int> f, siz;
DSU() {}
DSU(int n)
{
init(n);
}
void init(int n)
{
f.resize(n);
std::iota(f.begin(), f.end(), 0);
siz.assign(n, 1);
}
int find(int x)
{
while (x != f[x])
{
x = f[x] = f[f[x]];
}
return x;
}
bool same(int x, int y)
{
return find(x) == find(y);
}
bool merge(int x, int y)
{
x = find(x);
y = find(y);
if (x == y)
{
return false;
}
siz[x] += siz[y];
f[y] = x;
return true;
}
int size(int x)
{
return siz[find(x)];
}
} a, c;
pair<int, int> pa[1000];
void solve()
{
int n, m;
cin >> n >> m;
a.init(n + 1);
c.init(n + 1);
for (int i = 1; i <= m; i++)
{
cin >> e[i].x >> e[i].y >> e[i].w;
}
int q;
cin >> q;
int u, v;
for (int i = 0; i < q; i++)
{
cin >> u >> v;
pa[i] = {u, v};
auto [x2, y2, z2] = e[u];
auto [x1, y1, z1] = e[v];
a.merge(x1, y1);
a.merge(x2, y2);
f[u] = 1;
f[v] = 1;
}
for (int i = 1; i <= m; i++)
{
if (f[i])
v2.push_back(e[i]);
else
v1.push_back(e[i]);
}
sort(v1.begin(), v1.end());
int ans = 0;
for (auto tt : v1)
{
if (!a.same(tt.x, tt.y))
{
c.merge(tt.x, tt.y);
a.merge(tt.x, tt.y);
ans += tt.w;
}
else
{
v2.push_back(tt);
}
}
sort(v2.begin(), v2.end());
if ((a.size(1) != n))
{
cout << -1 << endl;
return;
}
int mm = 0;
for (int i = 1; i <= n; i++)
{
// cout << c.find(i) << ' ';
if (c.find(i) == i)
mm++;
}
// cout << endl;
DSU b;
b.init(n + 1);
int minn = 1e9;
map<int, int> ma;
for (int i = 0; i < 1 << q; i++)
{
ma.clear();
int sum = 0;
for (int j = 0; j < q; j++)
{
auto [xx, yy] = pa[j];
auto [x2, y2, z2] = e[yy];
auto [x1, y1, z1] = e[xx];
b.f[x1] = c.find(x1);
b.f[y1] = c.find(y1);
b.f[x2] = c.find(x2);
b.f[y2] = c.find(y2);
}
for (int j = 0; j < q; j++)
{
auto [xx, yy] = pa[j];
auto [x2, y2, z2] = e[yy];
auto [x1, y1, z1] = e[xx];
if ((i >> j) & 1)
{
b.merge(x2, y2);
if (!ma[yy])
sum += z2;
ma[yy] = 1;
}
else
{
b.merge(x1, y1);
if (!ma[xx])
sum += z1;
ma[xx] = 1;
}
}
int ttt = e[pa[0].first].x;
for (auto tt : v2)
{
// cout << b.f[tt.x] << ' ' << b.f[tt.y] << endl;
// cout << tt.x << ' ' << tt.y << endl;
if (!b.same(tt.x, tt.y))
{
b.merge(tt.x, tt.y);
sum += tt.w;
}
}
// cout << sum << endl;
minn = min(minn, sum);
}
cout << ans + minn << endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T = 1;
// cin>>T;
while (T--)
solve();
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5584kb
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: 89ms
memory: 29900kb
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: -100
Wrong Answer
time: 101ms
memory: 29504kb
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:
43100724
result:
wrong answer 1st lines differ - expected: '12148224', found: '43100724'