QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#265990#6503. DFS Order 3RomyStiQuEWA 85ms3844kbC++235.1kb2023-11-26 00:11:382023-11-26 00:11:39

Judging History

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

  • [2023-11-26 00:11:39]
  • 评测
  • 测评结果:WA
  • 用时:85ms
  • 内存:3844kb
  • [2023-11-26 00:11:38]
  • 提交

answer

//#pragma GCC optimize(1)
//#pragma GCC optimize(2)
//#pragma GCC optimize(3, "Ofast", "inline")
//#include <bits/stdc++.h>
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <chrono>
#include <random>
#define int long long
#define lowbit(x) ((x) & (~x))
#define endl '\n'
// ---------------------------------------------------------------------------------- //
//#define LOCAL
#ifdef LOCAL
#define debug(...) cerr << __VA_ARGS__ << '\n'
#define fileIO freopen("in.txt", "r", stdin); /*freopen("out.txt", "w", stdout);*/
#else
#define debug(...)
#define fileIO
#endif
// ---------------------------------------------------------------------------------- //
using namespace std;
using i128 = __int128;
using ull = unsigned long long;
using pii = pair<int, int>;
using pdd = pair<double, double>;
namespace fastIO {
	char buf[1 << 20], obuf[1 << 20], *p1, *p2, *p3 = obuf;
#define getchar() p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? 0 : *p1++
#define putchar(x) p3 - obuf < 1000000 ? (*p3++ = x) : (fwrite(obuf, p3 - obuf, 1, stdout), p3 = obuf, *p3++ = x)
	inline int read() {
		int x = 0, sgn = 1;
		char c = getchar();
		while (!isdigit(c) && c != '-') c = getchar();
		if (c == '-') sgn = -1, c = getchar();
		while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
		return x * sgn;
	}
	inline void write(int x) {
		if (x < 0) putchar('-'), x = -x;
		static char buf[20];
		static int len = 0;
		while (x) buf[len++] = x % 10 + '0', x /= 10;
		while (len--) putchar(buf[len]);
	}
	inline void flush() {
		fwrite(obuf, p3 - obuf, 1, stdout), exit(0);
	}
	//#undef getchar
	//#undef putchar
} using namespace fastIO;
const int N = 1e3 + 10;
const int P = 1e9 + 7, mod = 998244353;
const int INF = 0x3f3f3f3f;// 1,061,109,567
const int INF64 = 0x3f3f3f3f3f3f3f3f;// 4,557,430,888,798,830,399
const double pi = acos(-1);
//const double e = 2.718281828;
const double eps = 1e-9;

// ---------------------------------------------------------------------------------- //
int n, m, k;
int p[N];
//string s;
vector<int> D[N];
bool st[N];
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int fact[N], inv_fact[N];
//int h[N], e[N * 2], ne[N * 2], w[N * 2], idx;
// ---------------------------------------------------------------------------------- //

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

inline int my_rand(int a, int b) {
	uniform_int_distribution<int> dis(a, b);
	return dis(RNG);
}

inline void write128(i128 x) {
	if (x < 0) {
		putchar('-');
		x = -x;
	}
	if (x > 9) write128(x / 10);
	putchar(x % 10 + '0');
}

inline int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

int qpow(int a, int k, int p) {
	a %= p;
	int res = 1 % p;
	while (k) {
		if (k & 1) res = res * a % p;
		a = a * a % p;
		k >>= 1;
	}
	return res;
}

int inv(int a, int p) {
	return qpow(a, p - 2, p);
}

void init_C(int p) {
	fact[0] = inv_fact[0] = 1;
	for (int i = 1; i < N; i++) {
		fact[i] = fact[i - 1] * i % p;
		inv_fact[i] = inv_fact[i - 1] * inv(i, p) % p;
	}
}

inline int C(int a, int b, int p) {
	return a < b ? 0 : fact[a] * inv_fact[b] % p * inv_fact[a - b] % p;
}

//inline void add(int a, int b) {
//	G[a].push_back(b);
//	G[b].push_back(a);
//}

//inline void add(int a, int b) {
//	e[idx] = b, ne[idx] = h[a], h[a] = idx++;
//	e[idx] = a, ne[idx] = h[b], h[b] = idx++;
//}
//
//inline void add(int a, int b, int c) {
//	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
//	e[idx] = a, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
//}

int find(int x) {
	return x == p[x] ? x : p[x] = find(p[x]);
}

void unite(int a, int b) {
	a = find(a), b = find(b);
	if (a != b) p[a] = b;
}

void init_dsu(int n) {
	for (int i = 1; i <= n; i++) p[i] = i;
}

// ---------------------------------------------------------------------------------- //
void solve(int tc) {
	cin >> n;
	if (n == 1) return ;
	
	init_dsu(n);
	
	set<pii> s;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			int t; cin >> t;
			D[i].emplace_back(t);
		}
		int x = D[i][0], y = D[i][1];
		if (x > y) swap(x, y);
		s.insert({x, y});
		unite(x, y);
	}
	
	for (int i = 1; i < D[1].size(); i++) {
		int p1 = find(1), pi = find(D[1][i]), x = D[1][i], y;
		if (p1 != pi) {
			for (int j = 1; j < D[x].size(); j++) {
				if (pi != find(D[x][j])) {
					y = D[x][j];
					break;
				}
			}
			if (x > y) swap(x, y);
			s.insert({x, y});
			unite(x, y);
		}
	}
	
	for (auto I : s) cout << I.first << ' ' << I.second << endl;
//	cout << endl;
	
	for (int i = 1; i <= n; i++) D[i].clear();
}
// ---------------------------------------------------------------------------------- //

signed main(signed argc, char *argv[]) {
	
	cin.tie(nullptr);
	cout.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	fileIO
	
	int T = 1;
	cin >> T;
	for (int i = 1; i <= T; i++) solve(i);
	
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

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

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Wrong Answer
time: 85ms
memory: 3844kb

input:

20000
10
1 2 4 5 6 7 3 8 10 9
2 1 4 5 6 7 3 8 10 9
3 8 1 2 4 5 6 7 10 9
4 5 6 7 1 2 3 8 10 9
5 4 6 7 1 2 3 8 10 9
6 7 4 5 1 2 3 8 10 9
7 6 4 5 1 2 3 8 10 9
8 3 1 2 4 5 6 7 10 9
9 10 1 2 4 5 6 7 3 8
10 1 2 4 5 6 7 3 8 9
10
1 4 3 8 2 9 6 5 7 10
2 8 9 6 3 4 1 5 7 10
3 8 2 9 6 4 1 5 7 10
4 1 3 8 2 9 6 5...

output:

1 2
1 3
1 5
1 10
3 8
4 5
4 6
6 7
9 10
1 4
2 8
3 8
3 9
4 5
4 8
5 7
6 9
7 10
1 9
2 4
2 8
3 9
3 10
5 6
5 7
5 9
8 10
1 6
2 4
2 10
3 8
5 6
5 7
6 9
6 10
7 8
1 5
1 9
2 10
3 6
3 7
3 10
4 7
5 7
8 9
1 10
2 6
3 8
3 10
4 8
5 10
6 10
7 9
8 9
1 10
2 3
2 5
2 9
3 7
3 10
4 8
5 8
6 7
1 4
1 9
2 3
2 4
4 7
4 8
5 10
6 7
...

result:

wrong answer ord[1] didn't pass the test (test case 1)