QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#132682 | #5208. Jumbled Trees | Swarthmore# | WA | 1ms | 3596kb | C++17 | 5.3kb | 2023-07-31 02:17:34 | 2023-07-31 02:17:37 |
Judging History
answer
#include "bits/stdc++.h"
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define uid(a, b) uniform_int_distribution<int>(a, b)(rng)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef DEBUG
#define dbg(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << endl;
#else
#define dbg(x...)
#endif
int MOD = 1000000007;
const char nl = '\n';
const int MX = 501;
template<int SZ> struct DSU {
int parent[SZ], size[SZ];
DSU() {
F0R(i, SZ) parent[i] = i, size[i] = 0;
}
int get(int x) {
if (parent[x] != x) parent[x] = get(parent[x]);
return parent[x];
}
void unify(int x, int y) {
x = get(x); y = get(y);
if (x == y) return;
if (size[x] < size[y]) swap(x, y);
if (size[x] == size[y]) size[x]++;
parent[y] = x;
}
};
ll modExp(ll base, ll power) {
if (power == 0) {
return 1;
} else {
ll cur = modExp(base, power / 2); cur = cur * cur; cur = cur % MOD;
if (power % 2 == 1) cur = cur * base;
cur = cur % MOD;
return cur;
}
}
void solve() {
int N, M; cin >> N >> M >> MOD;
vector<array<int, 3>> eds(M);
F0R(i, M) {
cin >> eds[i][0] >> eds[i][1] >> eds[i][2];
eds[i][0]--; eds[i][1]--;
}
vector<vl> A(M, vl(M));
vl B(M); F0R(i, M) B[i] = eds[i][2];
F0R(i, M) {
DSU<MX> cur;
F0R(p, M) {
int j = (i+p)%M;
if (cur.get(eds[j][0]) != cur.get(eds[j][1])) {
A[j][i] = 1;
cur.unify(eds[j][0], eds[j][1]);
}
}
}
vector<vl> baseA = A;
vi col(M); iota(all(col), 0);
int ran = 0;
F0R(i, M) {
int v, br, bc, bv = 0;
FOR(r, i, M) {
FOR(c, i, M) {
if ((v = A[r][c]) > bv) {
br = r, bc = c, bv = v;
}
}
}
if (bv == 0) {
FOR(j, i, M) {
if (abs(B[j]) != 0) {
cout << -1 << nl; return;
}
}
break;
}
swap(A[i], A[br]);
swap(B[i], B[br]);
swap(col[i], col[bc]);
F0R(j, M) swap(A[j][i], A[j][bc]);
bv = modExp(A[i][i], MOD-2);
FOR(j, i+1, M) {
ll fac = A[j][i] * bv; fac %= MOD;
B[j] += MOD - ((fac * B[i]) % MOD); B[j] %= MOD;
FOR(k, i+1, M) {
A[j][k] += MOD - ((fac * A[i][k]) % MOD); A[j][k] %= MOD;
}
}
ran++;
}
vi x(M, 0);
F0Rd(i, ran) {
B[i] *= modExp(A[i][i], MOD-2); B[i] %= MOD;
x[col[i]] = B[i];
F0R(j, i) {
B[j] += MOD - ((A[j][i] * B[i]) % MOD); B[j] %= MOD;
}
}
cout << M << nl;
F0R(i, M) {
cout << x[i] << " ";
F0R(j, M) {
if (baseA[j][i]) cout << j+1 << " ";
}
cout << nl;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int T = 1;
// cin >> T;
while(T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3540kb
input:
3 3 101 1 2 30 2 3 40 3 1 50
output:
3 10 1 2 30 2 3 20 1 3
result:
ok Participant found an answer (3 trees) and jury found an answer (5 trees)
Test #2:
score: 0
Accepted
time: 1ms
memory: 3544kb
input:
2 2 37 1 2 8 1 2 15
output:
2 8 1 15 2
result:
ok Participant found an answer (2 trees) and jury found an answer (3 trees)
Test #3:
score: 0
Accepted
time: 1ms
memory: 3548kb
input:
5 4 5 1 3 1 2 3 2 2 5 3 4 1 4
output:
-1
result:
ok Both jury and participant did not find an answer
Test #4:
score: 0
Accepted
time: 0ms
memory: 3476kb
input:
10 15 997 4 3 459 9 7 94 9 8 767 10 2 877 5 8 258 3 4 166 8 5 621 8 10 619 9 1 316 10 5 516 3 10 125 1 7 961 3 6 500 4 10 976 3 4 842
output:
-1
result:
ok Both jury and participant did not find an answer
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 3596kb
input:
20 30 9973 1 10 696 3 8 2905 12 7 6609 20 10 1962 11 9 8430 19 2 412 6 3 6936 19 7 9113 14 15 5635 15 7 1770 13 10 3182 3 16 2625 17 1 7387 11 5 3700 9 15 1048 2 3 7717 12 10 8625 7 13 8141 5 14 2245 6 4 2819 18 19 8709 18 5 6191 17 10 7606 9 20 8626 17 4 8848 4 13 1073 10 8 2277 14 2 7714 11 8 5318...
output:
-1
result:
wrong answer Participant did not find an answer, while jury found (59 trees)