QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#389984 | #1817. AND Permutation | black_moonrise# | WA | 1ms | 4700kb | C++14 | 2.0kb | 2024-04-14 23:07:06 | 2024-04-14 23:07:08 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int maxn = 311;
int g[maxn][maxn], mx[maxn], my[maxn], n, og[maxn][maxn];
int mark[maxn], timer;
bool dfs(int x) {
if(mark[x] == timer) return true;
mark[x] = timer;
for(int y=1; y<=n; y++) {
if(!g[x][y]) continue;
if(!my[y] || dfs(my[y])) {
mx[x] = y;
my[y] = x;
return true;
}
}
return false;
}
bool match(int x) {
timer++;
return dfs(x);
}
typedef pair<int, int> pii;
int r, G, d[maxn][maxn];
vector<pii> E;
bool cmp(pii a, pii b) {
return d[a.first][a.second] < d[b.first][b.second];
}
int f[maxn];
int gf(int x) { return x == f[x] ? x : f[x] = gf(f[x]); }
void constructG(int u, int v) {
u = gf(u);
v = gf(v);
memset(g, 0, sizeof(g));
for(int x=1; x<=G; x++) for(int y=1; y<=n; y++) {
int t = gf(y);
if(t == u) t = v;
g[x][t] |= og[x][y];
}
}
int main() {
scanf("%d%d%d", &n, &r, &G);
memset(d, 0x1f, sizeof(d));
for(int i=1, u, v, w; i<=r; i++) {
scanf("%d%d%d", &u, &v, &w);
d[u][v] = d[v][u] = w;
}
for(int i=1; i<=G; i++) {
int x, y;
scanf("%d", &x);
while(x--) {
scanf("%d", &y);
og[i][y] = 1;
}
}
for(int i=1; i<=n; i++) f[i] = i;
constructG(0, 0);
for(int i=1; i<=G; i++) if(!match(i)) {
puts("-1");
return 0;
}
for(int i=1; i<=G; i++) printf("i= %d match= %d\n", i, mx[i]);
for(int i=1; i<=n; i++) for(int j=i+1; j<=n; j++) E.emplace_back(i, j);
sort(E.begin(), E.end(), cmp);
long long ans = 0;
printf("match= "); for(int j=1; j<=G; j++) printf("%d ", mx[j]); puts("");
for(auto e : E) {
int u = e.first, v = e.second;
if(gf(u) == gf(v)) continue;
printf("u= %d v= %d\n", u, v);
constructG(u, v); // u -> v
int y = gf(u), x = mx[y];
my[y] = mx[x] = 0;
if(x == 0 || match(x)) {
ans += d[u][v];
puts("merge");
f[gf(u)] = gf(v);
}
printf("match= "); for(int j=1; j<=G; j++) printf("%d ", mx[j]); puts("");
}
if(ans > 10000000) ans = -1;
printf("%lld\n", ans);
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 4700kb
input:
6 0 1 4 5 2 6
output:
i= 1 match= 2 match= 2 u= 1 v= 2 match= 2 u= 1 v= 3 match= 2 u= 1 v= 4 match= 2 u= 1 v= 5 match= 2 u= 1 v= 6 match= 2 u= 2 v= 3 merge match= 2 u= 2 v= 4 merge match= 2 u= 2 v= 5 merge match= 2 u= 2 v= 6 merge match= 2 -1
result:
wrong output format Expected integer, but "i=" found