QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#152914 | #3711. Floyd-Warshall | Zeoy | WA | 4ms | 18120kb | C++20 | 3.2kb | 2023-08-29 00:24:04 | 2023-08-29 00:24:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define Zeoy std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define rson id << 1 | 1
#define lson id << 1
#define int long long
#define mpk make_pair
#define endl '\n'
typedef unsigned long long ULL;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + 7;
const double eps = 1e-9;
const int N = 1e5 + 10, M = 4e5 + 10;
int n, m, q, fa[N], dep[N], dp[N][20], st[N];
int mp[N], idx, dis[210][N];
vector<array<int, 3>> edge;
vector<int> g[N], G[N];
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
void dfs(int u, int par)
{
dep[u] = dep[par] + 1;
dp[u][0] = par;
for (int i = 1; i <= 20; ++i)
dp[u][i] = dp[dp[u][i - 1]][i - 1];
for (auto v : g[u])
{
if (v == par)
continue;
dfs(v, u);
}
}
int lca(int u, int v)
{
if (dep[u] < dep[v])
swap(u, v);
for (int i = 20; i >= 0; --i)
{
if (dep[dp[u][i]] >= dep[v])
u = dp[u][i];
}
if (u == v)
return u;
for (int i = 20; i >= 0; --i)
{
if (dp[u][i] != dp[v][i])
{
u = dp[u][i];
v = dp[v][i];
}
}
return dp[u][0];
}
void bfs(int id, int st)
{
dis[id][st] = 0;
vector<int> vis(n + 10);
queue<int> q;
q.push(st);
vis[st] = true;
while (q.size())
{
int u = q.front();
q.pop();
for (auto v : G[u])
{
if (!vis[v])
{
vis[v] = true;
dis[id][v] = dis[id][u] + 1;
q.push(v);
}
}
}
}
void solve()
{
cin >> n >> m >> q;
for (int i = 1; i <= n; ++i)
fa[i] = i;
for (int i = 1; i <= m; ++i)
{
int u, v;
cin >> u >> v;
edge.push_back({u, v, i});
G[u].push_back(v);
G[v].push_back(u);
}
int rt = 0;
for (auto [x, y, id] : edge)
{
int fx = find(x), fy = find(y);
if (fx != fy)
{
fa[fx] = fy;
g[x].push_back(y);
g[y].push_back(x);
rt = x;
st[id] = true;
}
}
dfs(rt, 0);
for (int i = 1; i <= m; ++i)
{
if (!st[i])
{
auto [u, v, id] = edge[i - 1];
if (!mp[u])
{
mp[u] = ++idx;
bfs(idx, u);
}
if (!mp[v])
{
mp[v] = ++idx;
bfs(idx, v);
}
}
}
while (q--)
{
int u, v;
cin >> u >> v;
if (n == 1)
{
cout << 0 << endl;
continue;
}
int LCA = lca(u, v);
int ans = dep[u] + dep[v] - 2 * dep[LCA];
for (int i = 1; i <= idx; ++i)
ans = min(ans, dis[i][u] + dis[i][v]);
cout << ans << endl;
}
}
signed main(void)
{
Zeoy;
int T = 1;
// cin >> T;
while (T--)
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 16040kb
input:
4 5 3 1 2 1 3 1 4 2 3 3 4 2 2 2 3 2 4
output:
0 1 2
result:
ok 3 number(s): "0 1 2"
Test #2:
score: 0
Accepted
time: 4ms
memory: 11316kb
input:
1 2 1 1 1 1 1 1 1
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 4ms
memory: 16496kb
input:
5 7 10 5 3 1 2 4 1 2 5 3 4 2 2 1 3 5 5 4 5 3 2 4 4 5 3 3 5 2 4 4 2 5 3 4 2
output:
0 2 2 0 1 1 2 2 1 2
result:
ok 10 numbers
Test #4:
score: 0
Accepted
time: 1ms
memory: 18120kb
input:
5 8 10 5 1 1 3 4 5 2 3 1 5 4 2 2 2 2 3 2 1 2 3 4 5 1 1 2 2 5 3 3 5 3 4 4 5 1 1
output:
2 1 1 0 0 2 2 2 1 0
result:
ok 10 numbers
Test #5:
score: 0
Accepted
time: 4ms
memory: 16516kb
input:
5 8 10 4 1 5 2 3 5 3 1 5 1 5 1 4 2 2 5 3 1 3 2 2 1 3 2 3 3 1 5 4 5 4 3 4 3 2 5
output:
1 2 2 2 0 1 2 2 2 1
result:
ok 10 numbers
Test #6:
score: -100
Wrong Answer
time: 3ms
memory: 13540kb
input:
5 6 10 3 5 5 1 1 4 5 2 5 4 4 1 3 3 4 5 1 5 1 4 2 5 1 4 3 3 2 1 5 1 1 4
output:
2 1 1 1 1 1 2 2 1 1
result:
wrong answer 1st numbers differ - expected: '0', found: '2'