QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#429210#6520. Classic Problemdnialh#WA 4317ms276060kbC++232.9kb2024-06-02 03:35:262024-06-02 03:35:27

Judging History

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

  • [2024-06-02 03:35:27]
  • 评测
  • 测评结果:WA
  • 用时:4317ms
  • 内存:276060kb
  • [2024-06-02 03:35:26]
  • 提交

answer

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

#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define sq(a) ((a)*(a))

#define per(i,a,b) for (int i = (b)-1; i >= a; i--)
#define rep(i,a,b) for (int i=a; i<b; i++)

#define FOR(i,a,b) for (int i=a; i<b; i++)
#define F0R(i,a) for (int i=0; i<a; i++)
#define ROF(i,a,b) for (int i = (b)-1; i >= a; i--)
#define R0F(i,a) for (int i = (a)-1; i >= 0; i--)

#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << endl, 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << endl;
#define _ << " _ " <<

#define ll long long

template<class T> bool ckmin(T&a, T&b) { bool B = a > b; a = min(a,b); return B; }
template<class T> bool ckmax(T&a, T&b) { bool B = a < b; a = max(a,b); return B; }

typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ld,ld> pld;
typedef complex<ld> cd;

typedef vector<int> vi;
typedef vector<ld> vld;
typedef vector<vector<int>> vvi;

const ld PI = acos(-1.0);
const ld EPS = 1e-7;
const int MOD = 998244353;


struct UF {
	vi e;
	UF(int n) : e(n, -1) {}
	bool sameSet(int a, int b) { return find(a) == find(b); }
	int size(int x) { return -e[find(x)]; }
	int find(int x) { return e[x] < 0 ? x : e[x] = find(e[x]); }
	bool join(int a, int b) {
		a = find(a), b = find(b);
		if (a == b) return false;
		if (e[a] > e[b]) swap(a, b);
		e[a] += e[b]; e[b] = a;
		return true;
	}
};

void solve() {
  int n, m;
  cin >> n >> m;

  if (m == 0) {
    cout << (n - 1) << '\n';
    return;
  }

  vector<array<int, 3>> edges;

  set<pair<int, int>> bad;

  vi u(m), v(m), w(m);
  set<int> imp;
  F0R(i, m){
    cin >> u[i] >> v[i] >> w[i];
    imp.insert(u[i]);
    imp.insert(v[i]);
    imp.insert(u[i] + 1);
    imp.insert(v[i] + 1);
    imp.insert(u[i] - 1);
    imp.insert(v[i] - 1);

    bad.emplace(u[i], v[i]);
    bad.emplace(v[i], u[i]);
  }

  vi il;
  int z = 0;
  map<int, int> rev;
  for (auto uv : imp){
    if (uv <= 0 || uv > n) continue;

    il.pb(uv);
    rev[uv] = z;
    z += 1;
  }

  assert (z);

  ll out = n - z;

  vi deg(z);
  F0R(i, m){
    deg[rev[u[i]]] += 1;
    deg[rev[v[i]]] += 1;

    edges.push_back({w[i], rev[u[i]], rev[v[i]]});
  }

  F0R(i, z){
    int szz = 5 + 5 * deg[i];
    FOR(j, i - szz, i + szz + 1){
      if (j < 0 || j >= z) continue;
      if (bad.find({il[i], il[j]}) == bad.end()){
        edges.push_back({abs(il[i] - il[j]), i, j});
      }
    }
  }

  sort(all(edges));

  UF uf(z);

  for (auto e : edges){
    // cout << e[0] << " " << e[1] << "," << il[e[1]] << " " << e[2] << "," << il[e[2]] << '\n';
    if (uf.join(e[1], e[2])){
      out += e[0];
    }
  }

  cout << out << '\n';
}

int32_t main() { FAST
  int t;
  cin >> t;

  while (t--){
    solve();
  }

  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3644kb

input:

3
5 3
1 2 5
2 3 4
1 5 0
5 0
5 4
1 2 1000000000
1 3 1000000000
1 4 1000000000
1 5 1000000000

output:

4
4
1000000003

result:

ok 3 number(s): "4 4 1000000003"

Test #2:

score: -100
Wrong Answer
time: 4317ms
memory: 276060kb

input:

16
1000000000 0
447 99681
1 2 1000000000
1 3 1000000000
2 3 1000000000
1 4 1000000000
2 4 1000000000
3 4 1000000000
1 5 1000000000
2 5 1000000000
3 5 1000000000
4 5 1000000000
1 6 1000000000
2 6 1000000000
3 6 1000000000
4 6 1000000000
5 6 1000000000
1 7 1000000000
2 7 1000000000
3 7 1000000000
4 7 ...

output:

999999999
446000000000
732256441
1154158007
1167342035
1167818642
127
4
2186
1562
1176
5100
5
503
679
4

result:

wrong answer 4th numbers differ - expected: '999999680', found: '1154158007'