QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#181428#2169. 'S No ProblemAlleks_BWA 1ms6208kbC++142.4kb2023-09-16 18:48:262023-09-16 18:48:28

Judging History

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

  • [2023-09-16 18:48:28]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:6208kb
  • [2023-09-16 18:48:26]
  • 提交

answer

#include <bits/stdc++.h>
#define L 100005
using namespace std;

int n;
vector <pair <int, int>> G[L];
int root = 1, pathLength, maxPath, node1, node2, node3, diam1, diam2, sum;
int lev[L], t[L];
bool vis[L];
map <pair <int, int>, bool> mp;

void init() {
  for (int i = 1; i <= n; i++)
    vis[i] = false;
  pathLength = 0;
  maxPath = -L;
}

void switchEdge(int x, int y) {
  mp[{x, y}] = mp[{y, x}] = true;
}

void switchChain(int x, int y) {
  if (lev[x] < lev[y])
    swap(x, y);
  while (lev[x] > lev[y]) {
    switchEdge(x, t[x]);
    x = t[x];
  }
  while (x != y) {
    switchEdge(x, t[x]);
    switchEdge(y, t[y]);
    x = t[x];
    y = t[y];
  }
}

void DFS_1(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node1 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      lev[it.first] = lev[node] + 1;
      t[it.first] = node;
      pathLength += it.second;
      DFS_1(it.first);
      pathLength -= it.second;
    }
  }
}

void DFS_2(int node) {
  vis[node] = true;
  if (maxPath < pathLength) {
    maxPath = pathLength;
    node2 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      pathLength += it.second;
      DFS_2(it.first);
      pathLength -= it.second;
    }
  }
}

void DFS_3(int node) {
  vis[node] = true;
  if (maxPath <= pathLength) {
    maxPath = pathLength;
    node3 = node;
  }
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      int cost = it.second;
      if (mp[{node, it.first}])
        cost = -cost;
      pathLength += cost;
      DFS_3(it.first);
      pathLength -= cost;
    }
  }
}

void DFS_4(int node) {
  vis[node] = true;
  maxPath = max(maxPath, pathLength);
  for (auto it : G[node]) {
    if (!vis[it.first]) {
      int cost = it.second;
      if (mp[{node, it.first}])
        cost = -cost;
      pathLength += cost;
      DFS_4(it.first);
      pathLength -= cost;
    }
  }
}

int main(){
  cin >> n;
  for (int i = 1; i < n; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    G[a].push_back({b, c});
    G[b].push_back({a, c});
    sum += c;
  }

  init();
  DFS_1(root);

  init();
  DFS_2(node1);
  diam1 = maxPath;

  switchChain(node1, node2);

  init();
  DFS_3(root);

  init();
  DFS_4(node3);
  diam2 = maxPath;

  cout << sum * 2 - diam1 - diam2 << "\n";
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 6056kb

input:

5
1 2 1
1 3 2
1 4 3
1 5 4

output:

10

result:

ok single line: '10'

Test #2:

score: 0
Accepted
time: 1ms
memory: 5996kb

input:

6
6 2 1
4 6 1
3 1 1
1 5 1
1 6 10

output:

15

result:

ok single line: '15'

Test #3:

score: 0
Accepted
time: 1ms
memory: 6208kb

input:

6
1 3 1
3 2 1
3 4 10
5 4 1
4 6 1

output:

15

result:

ok single line: '15'

Test #4:

score: 0
Accepted
time: 1ms
memory: 6056kb

input:

6
1 3 1
2 3 1
3 5 1
4 5 1
5 6 1

output:

6

result:

ok single line: '6'

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 6180kb

input:

6
1 2 5
3 2 3
2 4 1
4 5 2
4 6 4

output:

20

result:

wrong answer 1st lines differ - expected: '16', found: '20'