QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#132685#5208. Jumbled TreesSwarthmore#WA 1ms3604kbC++175.3kb2023-07-31 02:19:552023-07-31 02:19:58

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-31 02:19:58]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3604kb
  • [2023-07-31 02:19:55]
  • 提交

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]%M;
    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: 0
Wrong Answer
time: 1ms
memory: 3604kb

input:

3 3 101
1 2 30
2 3 40
3 1 50

output:

3
50 1 2 
52 2 3 
51 1 3 

result:

wrong answer Wrong value of edge 0 (1-based): expected = 30, actual = 0