QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#683560#6473. Cat and MouseforgotmyhandleTL 0ms0kbC++143.8kb2024-10-27 21:51:552024-10-27 21:51:55

Judging History

你现在查看的是最新测评结果

  • [2024-10-27 21:51:55]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:0kb
  • [2024-10-27 21:51:55]
  • 提交

answer

#include <iostream>
#include <string.h>
#include <vector>
#include <queue>
#include <map>
using namespace std;
int tc;
int n, X;
int head[50005], nxt[100005], to[100005], ew[100005], ecnt;
void add(int u, int v, int ww) { to[++ecnt] = v, nxt[ecnt] = head[u], head[u] = ecnt, ew[ecnt] = ww; }
int eu[50005], ev[50005];
int f[50005], g[50005];
vector<pair<int, int> > G[100005];
map<pair<int, int>, int> mp;
int deg[50005];
struct node {
    int x, dis;
};
bool vis[100005];
int dist[100005];
bool operator<(node a, node b) { return a.dis > b.dis; }
priority_queue<node> q;
void dijkstra() {
    while (!q.empty()) {
        node tmp = q.top();
        q.pop();
        int x = tmp.x;
        if (vis[x]) 
            continue;
        vis[x] = 1;
        for (auto i : G[x]) {
            int v = i.first, w = i.second;
            if (dist[v] > dist[x] + w) 
                q.push((node) { v, dist[v] = dist[x] + w });
        }
    }
}
int dep[100005], fa[100005];
void dfs(int x, int fa) {
    dep[x] = dep[fa] + 1, ::fa[x] = fa;
    for (int i = head[x]; i; i = nxt[i]) to[i] != fa ? dfs(to[i], x) : void();
}
inline int F(int x, int y) { return (f[x] == y ? g[x] : f[x]); }
int main() {
    cin >> tc;
    while (tc--) {
        memset(head, 0, sizeof head);
        memset(deg, 0, sizeof deg);
        memset(vis, 0, sizeof vis);
        ecnt = 0, mp.clear();
        dep[0] = -1;
        cin >> n >> X;
        for (int i = 1; i < n; i++) {
            int &u = eu[i], &v = ev[i];
            cin >> u >> v;
            mp[make_pair(u, v)] = i * 2 - 1;
            mp[make_pair(v, u)] = i * 2;
            add(u, v, i);
            add(v, u, i);
            ++deg[u], ++deg[v];
        }
        dfs(1, 0);
        for (int i = 1; i <= n * 2; i++) G[i].clear();
        for (int i = 1; i <= n; i++) {
            g[i] = f[i] = 0;
            for (int j = head[i]; j; j = nxt[j]) {
                if (ew[j] > ew[f[i]]) 
                    g[i] = f[i], f[i] = j;
                else if (ew[j] > ew[g[i]]) 
                    g[i] = j;
            }
            f[i] = to[f[i]];
            g[i] = to[g[i]];
            // cout << i << " " << f[i] << " asdf\n";
        }
        memset(dist, 63, sizeof dist);
        for (int i = 1; i <= n; i++) {
            for (int j = head[i]; j; j = nxt[j]) {
                int v = to[j];
                if (deg[v] == 1) {
                    int x = mp[make_pair(i, v)];
                    q.push((node) { x, dist[x] = 0 });
                } else {
                    int w = F(v, i);
                    // cout << v << " " << i << " " << F(v, i) << "\n";
                    G[mp[make_pair(v, w)]].emplace_back(make_pair(mp[make_pair(i, v)], 1));
                    if (f[w] == v) 
                        G[mp[make_pair(w, v)]].emplace_back(make_pair(mp[make_pair(i, v)], 4));
                }
            }
        }
        dijkstra();
        // for (int i = 1; i < n; i++) cout << eu[i] << " " << ev[i] <<" " << dist[mp[make_pair(eu[i], ev[i])]] << "\n";
        // for (int i = 1; i < n; i++) cout << ev[i] << " " << eu[i] <<" " << dist[mp[make_pair(ev[i], eu[i])]] << "\n";
        int p = X, ans = n + 1;
        for (int i = 0; i <= n; i++) {
            for (int j = head[p]; j; j = nxt[j]) {
                int v = to[j];
                if (fa[p] == v && dep[v] <= i) 
                    ans = min(ans, dist[mp[make_pair(v, p)]] + i);
                if (fa[p] != v && dep[v] + 1 <= i) 
                    ans = min(ans, dist[mp[make_pair(v, p)]] + i);
                // if (dep[v] + (fa[p] != v) <= i + 1) 
                //     ans = min(ans, dist[mp[make_pair(v, p)]] + i);
            }
            p = f[p];
        }
        cout << ans << "\n";
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Time Limit Exceeded

input:

100
50000 11576
13519 31672
47035 48424
15051 40308
19661 34367
9452 17569
390 9986
26705 26923
37100 10713
25643 42676
9267 19233
49342 37739
6298 3807
5751 22924
25960 4439
33941 13190
12456 3487
29455 14728
49187 6717
36264 16848
46193 37225
30433 49509
2672 30522
12241 13826
42170 21723
19926 39...

output:

6560
12932
14271
19557
38634
46926
12218
21212
19279
8468
21912
2494
21988
15817
26345
16510
5965
6981
10344
3792
38960
8434
24402
26580
44114
5864
28880
14774
8806
23052
2964
34968
14304
9782
27897
23955
1070
15266
1734
17352
13635
9744
18308
32312
14708
7506
18284
8966
12870
2692
20240
34538
13611...

result: