Submission #1034821


Source Code Expand

#include <bits/stdc++.h>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a) { cout << #a << " = " << a << endl; }
template<class T1, class T2> inline bool minimize(T1 &a, T2 b) { return b < a && (a = b, 1); }
template<class T1, class T2> inline bool maximize(T1 &a, T2 b) { return a < b && (a = b, 1); }
template<class T> void operator>> (istream& ist, vector<T>& vs) { for(auto& e: vs) cin >> e; }

typedef long long ll;
int const inf = 1<<29;

namespace tree {
struct union_find {
  vector<int> par, rank, size;
  int compnum;

  union_find(int N) {
    compnum = N;
    par.resize(N), rank.resize(N), size.resize(N);
    for(int i=0; i<N; i++) {
      par[i] = i;
      rank[i] = 0;
      size[i] = 1;
    }
  }

  int root(int x) {
    return par[x] == x ? x : par[x] = root(par[x]);
  }

  void unite(int x, int y) {
    x = root(x), y = root(y);
    if(x == y) return;
    if(rank[x] < rank[y]) {
      par[x] = y, size[y] += size[x];
    } else {
      par[y] = x, size[x] += size[y];
      if(rank[x] == rank[y]) rank[x]++;
    }
    compnum--;
  }

  int operator[](int x) { return root(x); }
  void operator()(int x, int y) { return unite(x, y); }

  bool same(int x, int y) { return root(x) == root(y); }
  int size_of(int x) { return size[root(x)]; }
  int num_of_comps() { return compnum; }
};
}

int dist[4000][4000];
vector<pair<int, int>> G[4000];
tuple<int, int, int> es[4000];

void dfs(int curr, int par, int start, int max) {
  dist[curr][start] = max;
  for(auto const& e: G[curr]) {
    if(e.first != par) dfs(e.first, curr, start, std::max(max, e.second));
  }
};

int main() {

  int N, M; cin >> N >> M;

  rep(i, M) {
    int a, b, c; scanf("%d%d%d", &a, &b, &c);
    a--, b--;
    es[i] = make_tuple(c, a, b);
    G[a].push_back({b, c});
    G[b].push_back({a, c});
  }

  sort(es, es+N);

  tree::union_find uf(N);

  ll rawMSTWeight = 0;

  rep(i, M) {
    int a, b, c; tie(c, a, b) = es[i];
    if(!uf.same(a, b)) {
      uf.unite(a, b);
      rawMSTWeight += c;
    }
  }

  rep(i, N) {
    dfs(i, -1, i, 0);
  }

  int Q; cin >> Q;
  rep(i, Q) {
    int s, t; scanf("%d%d", &s, &t); s--, t--;
    cout << rawMSTWeight - dist[s][t] << endl;
  }
  
  return 0;
}

Submission Info

Submission Time
Task A - Graph
User motxx
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2477 Byte
Status WA
Exec Time 3165 ms
Memory 262528 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:74:45: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int a, b, c; scanf("%d%d%d", &a, &b, &c);
                                             ^
./Main.cpp:101:36: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
     int s, t; scanf("%d%d", &s, &t); s--, t--;
                                    ^

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 0 / 200 0 / 300 0 / 200
Status
WA × 1
MLE × 1
WA × 2
TLE × 1
MLE × 2
RE × 7
WA × 4
TLE × 1
MLE × 2
RE × 14
WA × 5
TLE × 1
MLE × 2
RE × 21
Set Name Test Cases
Sample sample_1.txt, sample_2.txt
subtask1 sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
subtask2 sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt
All sample_1.txt, sample_2.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt, subtask_2_1.txt, subtask_2_2.txt, subtask_2_3.txt, subtask_2_4.txt, subtask_2_5.txt, subtask_2_6.txt, subtask_2_7.txt, subtask_2_8.txt, subtask_3_1.txt, subtask_3_2.txt, subtask_3_3.txt, subtask_3_4.txt, subtask_3_5.txt, subtask_3_6.txt, subtask_3_7.txt, subtask_3_8.txt
Case Name Status Exec Time Memory
sample_1.txt WA 3 ms 384 KB
sample_2.txt MLE 442 ms 262528 KB
subtask_1_1.txt MLE 432 ms 262528 KB
subtask_1_10.txt RE 113 ms 512 KB
subtask_1_11.txt WA 3 ms 384 KB
subtask_1_2.txt RE 113 ms 512 KB
subtask_1_3.txt RE 113 ms 512 KB
subtask_1_4.txt TLE 3165 ms 122496 KB
subtask_1_5.txt RE 113 ms 512 KB
subtask_1_6.txt RE 113 ms 512 KB
subtask_1_7.txt RE 113 ms 512 KB
subtask_1_8.txt WA 419 ms 63104 KB
subtask_1_9.txt RE 113 ms 512 KB
subtask_2_1.txt RE 113 ms 512 KB
subtask_2_2.txt RE 113 ms 512 KB
subtask_2_3.txt RE 113 ms 512 KB
subtask_2_4.txt RE 113 ms 512 KB
subtask_2_5.txt WA 432 ms 63104 KB
subtask_2_6.txt RE 113 ms 512 KB
subtask_2_7.txt RE 113 ms 512 KB
subtask_2_8.txt RE 113 ms 512 KB
subtask_3_1.txt RE 113 ms 512 KB
subtask_3_2.txt RE 113 ms 512 KB
subtask_3_3.txt WA 886 ms 64384 KB
subtask_3_4.txt RE 113 ms 512 KB
subtask_3_5.txt RE 113 ms 512 KB
subtask_3_6.txt RE 113 ms 512 KB
subtask_3_7.txt RE 113 ms 512 KB
subtask_3_8.txt RE 113 ms 512 KB