Submission #1002137


Source Code Expand

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define FOR(i,k,n) for (int (i)=(k); (i)<(n); ++(i))
#define rep(i,n) FOR(i,0,n)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fst first
#define snd second
#define all(v) begin(v), end(v)
#define debug(x) cerr<< #x <<": "<<x<<endl
#define debug2(x,y) cerr<< #x <<": "<< x <<", "<< #y <<": "<< y <<endl

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<ll> vll;
typedef vector<vector<ll> > vvll;
typedef vector<char> vc;
typedef vector<vector<char> > vvc;
typedef vector<double> vd;
typedef vector<vector<double> > vvd;
template<class T> using vv=vector<vector< T > >;
typedef deque<int> di;
typedef deque<deque<int> > ddi;
typedef deque<bool> db;
typedef deque<deque<bool> > ddb;

// cout pair
template<typename T1, typename T2> ostream& operator<<(ostream& s, const pair<T1, T2>& p) {
  s << p.first << " " << p.second << "\n"; return s;
}

// cout vector<pair>
template<typename T1, typename T2> ostream& operator<<(ostream& s, const vector<pair<T1, T2> >& vp) {
  int len = vp.size(); s << "\n";
  for (int i = 0; i < len; ++i) { s << vp[i]; }
    s << "\n"; return s;
}

// cout vector
template<typename T> ostream& operator<<(ostream& s, const vector<T>& v) {
  int len = v.size(); s << "\n";
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << "\t";
  }
  s << "\n"; return s;
}

// cout deque
template<typename T> ostream& operator<<(ostream& s, const deque<T>& v) {
  int len = v.size(); s << "\n";
  for (int i = 0; i < len; ++i) {
    s << v[i]; if (i < len - 1) s << "\t";
  }
  s << "\n"; return s;
}

// cout 2-dimentional vector
template<typename T> ostream& operator<<(ostream& s, const vector< vector<T> >& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) { s << vv[i]; }
  return s;
}

// cout 2-dimentional deque
template<typename T> ostream& operator<<(ostream& s, const deque< deque<T> >& vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) { s << vv[i]; }
  return s;
}

struct UF {
  vector<int> par; // parent
  vector<int> sizes;
  UF(int n) : par(n), sizes(n, 1) {
    for (int i = 0; i < n; ++i) {
      par[i] = i;
    }
  }
  int find(int x) {
    if (x == par[x]) return x;
    return par[x] = find(par[x]);
  }
  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y) {
      return;
    }
    if (sizes[x] < sizes[y]) {
      swap(x, y);
    }
    par[y] = x;
    sizes[x] += sizes[y];
    sizes[y] = 0;
  }
  bool same(int x, int y) {
    return find(x) == find(y);
  }
  int size(int x) {
    return sizes[find(x)];
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vvi edge(m+1, vi(3));
  rep (i, m) {
    cin >> edge[i][1] >> edge[i][2] >> edge[i][0];
    edge[i][1] -= 1; edge[i][2] -= 1;
  }
  edge[m][0] = 0;
  sort(all(edge));
  int q;
  cin >> q;
  vll ans(q, 0);
  vvi edge_ = edge;
  rep (j, q) {
    int s, t;
    cin >> s >> t;
    s -= 1; t -= 1;
    edge_[0][1] = s;
    edge_[0][2] = t;
    UF uf(n);
    rep (i, m+1) {
      if (!(uf.same(edge_[i][1], edge_[i][2]))) {
        ans[j] += edge_[i][0];
        uf.unite(edge_[i][1], edge_[i][2]);
      }
      if (uf.size(s) == n) {
        break;
      }
    }
  }
  rep (i, q) {
    printf("%lld\n", ans[i]);
  }

  return 0;
}

Submission Info

Submission Time
Task A - Graph
User tspcx
Language C++14 (Clang 3.8.0)
Score 500
Code Size 3813 Byte
Status TLE
Exec Time 3158 ms
Memory 44800 KB

Judge Result

Set Name Sample subtask1 subtask2 All
Score / Max Score 0 / 0 200 / 200 300 / 300 0 / 200
Status
AC × 2
AC × 12
AC × 21
AC × 21
TLE × 8
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 256 KB
sample_2.txt AC 3 ms 256 KB
subtask_1_1.txt AC 5 ms 384 KB
subtask_1_10.txt AC 567 ms 22144 KB
subtask_1_11.txt AC 3 ms 256 KB
subtask_1_2.txt AC 115 ms 4608 KB
subtask_1_3.txt AC 1159 ms 44032 KB
subtask_1_4.txt AC 14 ms 768 KB
subtask_1_5.txt AC 17 ms 896 KB
subtask_1_6.txt AC 283 ms 11264 KB
subtask_1_7.txt AC 1160 ms 44032 KB
subtask_1_8.txt AC 14 ms 768 KB
subtask_1_9.txt AC 31 ms 1408 KB
subtask_2_1.txt AC 2451 ms 44032 KB
subtask_2_2.txt AC 2202 ms 44160 KB
subtask_2_3.txt AC 2193 ms 44032 KB
subtask_2_4.txt AC 2346 ms 44032 KB
subtask_2_5.txt AC 393 ms 768 KB
subtask_2_6.txt AC 1214 ms 2560 KB
subtask_2_7.txt AC 1388 ms 11264 KB
subtask_2_8.txt AC 2539 ms 44032 KB
subtask_3_1.txt TLE 3158 ms 44800 KB
subtask_3_2.txt TLE 3158 ms 44800 KB
subtask_3_3.txt TLE 3154 ms 1536 KB
subtask_3_4.txt TLE 3154 ms 2176 KB
subtask_3_5.txt TLE 3156 ms 22912 KB
subtask_3_6.txt TLE 3157 ms 33920 KB
subtask_3_7.txt TLE 3157 ms 44800 KB
subtask_3_8.txt TLE 3158 ms 44800 KB