Submission #1030612


Source Code Expand

#include<iostream>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<functional>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cassert>
#include<ctime>
using namespace std;

#define mind(a,b) (a>b?b:a)
#define maxd(a,b) (a>b?a:b)
#define absd(x) (x<0?-(x):x)
#define pow2(x) ((x)*(x))
#define rep(i,n) for(int i=0; i<n; ++i)
#define repr(i,n) for(int i=n-1; i>=0; --i)
#define repl(i,s,n) for(int i=s; i<=n; ++i)
#define replr(i,s,n) for(int i=n; i>=s; --i)
#define repf(i,s,n,j) for(int i=s; i<=n; i+=j)
#define repe(e,obj) for(auto e : obj)

#define SP << " " <<
#define COL << " : " <<
#define COM << ", " <<
#define ARR << " -> " <<
#define PNT(STR) cout << STR << endl
#define POS(X,Y) "(" << X << ", " << Y << ")"
#define DEB(A) " (" << #A << ") " << A
#define DEBREP(i,n,val) for(int i=0; i<n; ++i) cout << val << " "; cout << endl
#define ALL(V) (V).begin(), (V).end()
#define INF 1000000007
#define INFLL 1000000000000000007LL
#define EPS 1e-9

typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
#define P_TYPE int
typedef pair<P_TYPE, P_TYPE> P;
typedef pair<P, P_TYPE> PI;
typedef pair<P_TYPE, P> IP;
typedef pair<P, P> PP;
typedef priority_queue<P, vector<P>, greater<P> > pvqueue;

#define N 4003

int parent[N];
int root(int x) {
  if(x != parent[x]) x = parent[x] = root(parent[x]);
  return x;
}
void unite(int x, int y) {
  int px = root(x), py = root(y);
  if(px < py) {
    parent[py] = px;
  } else {
    parent[px] = py;
  }
}

vector<P> g[N];
ll maxi[N][N];
void dfs(int s, int v, int prev, int cost) {
  maxi[s][v] = cost;
  rep(i, g[v].size()) {
    P &e = g[v][i];
    if(prev != e.first) {
      dfs(s, e.first, v, maxd(cost, e.second));
    }
  }
}

int main() {
  int n, m; cin >> n >> m;
  vector<IP> v;
  ll su = 0;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    v.push_back(IP(c, P(a, b)));
  }
  sort(ALL(v));
  rep(i, n) parent[i+1] = i+1;
  rep(i, m) {
    IP &e = v[i];
    int a = e.second.first, b = e.second.second, c = e.first;
    if(root(a) != root(b)) {
      unite(a, b);
      su += c;
      g[a].push_back(P(b, c));
      g[b].push_back(P(a, c));
    }
  }
  rep(i, n) {
    dfs(i+1, i+1, -1, 0);
  }
  int q;cin >> q;
  rep(i, q) {
    int s, t; cin >> s >> t;
    cout << su - maxi[s][t] << endl;
  }
  return 0;
}

Submission Info

Submission Time
Task A - Graph
User yaketake08
Language C++14 (GCC 5.4.1)
Score 700
Code Size 2555 Byte
Status AC
Exec Time 1362 ms
Memory 131436 KB

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 200 / 200 300 / 300 200 / 200
Status
AC × 2
AC × 12
AC × 21
AC × 29
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 AC 3 ms 384 KB
sample_2.txt AC 3 ms 384 KB
subtask_1_1.txt AC 4 ms 768 KB
subtask_1_10.txt AC 675 ms 127984 KB
subtask_1_11.txt AC 3 ms 384 KB
subtask_1_2.txt AC 527 ms 126072 KB
subtask_1_3.txt AC 879 ms 130284 KB
subtask_1_4.txt AC 456 ms 125696 KB
subtask_1_5.txt AC 491 ms 125696 KB
subtask_1_6.txt AC 583 ms 126836 KB
subtask_1_7.txt AC 868 ms 130284 KB
subtask_1_8.txt AC 461 ms 125696 KB
subtask_1_9.txt AC 498 ms 125760 KB
subtask_2_1.txt AC 893 ms 130284 KB
subtask_2_2.txt AC 887 ms 130284 KB
subtask_2_3.txt AC 891 ms 130284 KB
subtask_2_4.txt AC 902 ms 130284 KB
subtask_2_5.txt AC 469 ms 125696 KB
subtask_2_6.txt AC 540 ms 125820 KB
subtask_2_7.txt AC 594 ms 126836 KB
subtask_2_8.txt AC 883 ms 130284 KB
subtask_3_1.txt AC 1359 ms 131436 KB
subtask_3_2.txt AC 1362 ms 131436 KB
subtask_3_3.txt AC 948 ms 126976 KB
subtask_3_4.txt AC 986 ms 127040 KB
subtask_3_5.txt AC 1166 ms 129136 KB
subtask_3_6.txt AC 1261 ms 130284 KB
subtask_3_7.txt AC 1361 ms 131436 KB
subtask_3_8.txt AC 1359 ms 131436 KB