QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#305602#5208. Jumbled TreesLaStataleBlueTL 2483ms13440kbC++236.7kb2024-01-15 18:11:472024-01-15 18:11:47

Judging History

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

  • [2024-01-15 18:11:47]
  • 评测
  • 测评结果:TL
  • 用时:2483ms
  • 内存:13440kb
  • [2024-01-15 18:11:47]
  • 提交

answer

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename T> T bpow(T x, size_t n) {
    if(n == 0) return T(1);
    else{auto t=bpow(x, n/2); t=t*t; return n%2 ? x*t : t;}
}
int m;
struct modular {
    int r; typedef modular mo;
    modular(): r(0) {}
    modular(int64_t rr): r(rr%m){if(r<0)r+=m;}
    mo inv() const {return bpow(*this, m - 2);}
    mo operator-()const{return r ? m - r : 0;}
    mo operator*(const mo &t)const{return (ll)r*t.r%m;}
    mo operator/(const mo &t)const{return *this*t.inv();}
    mo operator+=(const mo &t){
        r += t.r; if(r >= m) r -= m; return *this;}
    mo operator-=(const mo &t){
        r -= t.r; if(r < 0) r += m; return *this;}
    mo operator+(const mo &t)const{return mo(*this)+=t;}
    mo operator-(const mo &t)const{return mo(*this)-=t;}
    mo operator*=(const mo &t){return *this=*this*t;}
    bool operator==(const mo &t)const{return r==t.r;}
    bool operator>(const mo &t)const{return r>t.r;}
    bool operator<=(const double &t)const{return r<=int(t);}
    bool operator>(const double &t)const{return r>int(t);}
};
modular fabs(modular x){return x.r;}

typedef modular T; //oppure modular<mod>
typedef vector<T> vd;
const double eps = 1e-12;
vi col; //globale per ricavare il sottospazio
int solveLinear(vector<vd>& A, vd& b, vd& x) {
    int n = sz(A), m = sz(x), rank = 0, br, bc;
    if (n) assert(sz(A[0]) == m);
    col.resize(m); iota(all(col), 0);
    rep(i,0,n) {
        T v, bv = 0;
        rep(r,i,n) rep(c,i,m)
        if ((v = fabs(A[r][c])) > bv)
            br = r, bc = c, bv = v;
        if (bv <= eps) {
            rep(j,i,n) if (fabs(b[j]) > eps) return -1;
            break;
        }
        swap(A[i], A[br]); swap(b[i], b[br]);
        swap(col[i], col[bc]);
        rep(j,0,n) swap(A[j][i], A[j][bc]);
        bv = T(1)/A[i][i];
        rep(j,i+1,n) {
            T fac = A[j][i] * bv;
            b[j] -= fac * b[i];
            rep(k,i+1,m) A[j][k] -= fac*A[i][k];
        }
        rank++;
    }
    x.assign(m, T(0));
    for (int i = rank; i--;) {
        b[i] = b[i]/A[i][i];
        rep(z,i+1,m){ //(*)per ricavare il sottospazio
            A[i][z] = A[i][z]/A[i][i];
        }
        x[col[i]] = b[i];
        rep(j,0,i) {
            rep(z,i+1,m){ //(*)per ricavare il sottospazio
                A[j][z] -= A[j][i] * A[i][z];
            }
            b[j] -= A[j][i] * b[i];
        }
    }
    return rank; // (multiple solutions if rank < m)
}

void solve(){
    int n,e;
    cin>>n>>e>>m;
    
    vd b,x;
    vector<vd> A;
    
    vector<array<int,4>> edges(e);
    vector ok(e,false),st(e,false);
    vd curr(e);
    vector grafo(n+1,vector<array<int,3>>());
    
    for(int i=0;i<e;i++){
        int u,v,x;
        cin>>u>>v>>x;
        b.push_back(x);
        edges[i]={u,v,(int)grafo[u].size(),(int)grafo[v].size()};
        grafo[u].push_back({v,false,i});
        grafo[v].push_back({u,false,i});
    }
    
    auto set = [&](int id,bool val){
        auto [u,v,ind1,ind2] = edges[id];
        grafo[u][ind1][1]=val;
        grafo[v][ind2][1]=val;
        curr[id]=val;
    };
    
    vector vis=vector(n,false);
    auto dfs = [&](auto &dfs,int nodo)->void{
        vis[nodo]=true;
        for(auto [to,act,id] : grafo[nodo]){
            if(!vis[to]){
                st[id]=true;
                set(id,true);
                dfs(dfs,to);
            }
        }
    };
    dfs(dfs,1);
    
    vector<int> cycle;
    auto findcycle = [&](auto &findcycle,int nodo,int last,int st)->bool{
        if(nodo==st)return true;
        
        bool res=false;
        for(auto [to,act,id] : grafo[nodo]){
            if(act && to!=last){
                if(findcycle(findcycle,to,nodo,st)){
                    cycle.push_back(id);
                    res=true;
                }
            }
        }
        return res;
    };
    
    for(int i=0;i<e;i++){
        if(!st[i]){
            //cout<<"processo "<<i<<"\n";
            cycle.clear();
            auto [u,v] = tie(edges[i][0],edges[i][1]);
            findcycle(findcycle,u,-1,v);
            
            set(i,true);
            for(auto j : cycle){
                //cout<<j<<" ";
                if(ok[j])continue;
                ok[j]=true;
                set(j,false);
                A.push_back(curr);
                set(j,true);
            }
            //cout<<"cicle \n";
            set(i,false);
        }
    }
    
    
    A.push_back(curr);
    int last=-1;
    
    for(int i=0;i<e;i++){
        if(!st[i]){
            cycle.clear();
            auto [u,v] = tie(edges[i][0],edges[i][1]);
            findcycle(findcycle,u,-1,v);
            
            set(i,true);
            
            bool ok=false;
            for(auto j : cycle){
                if(st[j]){
                    set(j,false);
                    ok=true;
                    break;
                }
            }
            if(!ok){
                set(cycle[0]==last ? cycle[1] : cycle[0],false);
            }
            
            A.push_back(curr);
            last=i;
        }
    }
    
    while(A.size()!=2*e){
        vector<int> opz;
        for(int i=0;i<e;i++){
            if(curr[i].r==0)opz.push_back(i);
        }
        if(opz.size()==0)break;
        
        int i = opz[rand()%opz.size()];
        
        cycle.clear();
        auto [u,v] = tie(edges[i][0],edges[i][1]);
        findcycle(findcycle,u,-1,v);
        
        set(i,true);
        
        bool ok=false;
        
        set(cycle[rand()%cycle.size()],false);
        
        A.push_back(curr);
        last=i;
    }
    /*
    for(auto i : A){
        for(auto j : i)cout<<j.r<<" ";
        cout<<"\n";
    }*/
    
    vector tmp = vector(e,vd(A.size()));
    
    for(int i=0;i<(int)A.size();i++){
        for(int j=0;j<(int)A[i].size();j++){
            tmp[j][i]=A[i][j];
        }
    }
    swap(A,tmp);
    x.resize(A[0].size());
    
    if(solveLinear(A,b,x)==-1){
        cout<<"-1\n";
    }else{
        cout<<x.size()<<"\n";
        for(int i=0;i<(int)x.size();i++){
            cout<<x[i].r<<" ";
            for(int j=0;j<(int)tmp[i].size();j++)if(tmp[i][j]==1){
                cout<<j+1<<" ";
            }
            cout<<"\n";
        }
    }
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int t=1;
    //cin>>t;
    for(int i=1;i<=t;i++)solve();
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

6
30 2 3 
20 1 3 
10 1 2 
0 2 3 
0 1 3 
0 2 3 

result:

ok Participant found an answer (6 trees) and jury found an answer (5 trees)

Test #2:

score: 0
Accepted
time: 0ms
memory: 3628kb

input:

2 2 37
1 2 8
1 2 15

output:

4
15 2 
8 1 
0 2 
0 1 

result:

ok Participant found an answer (4 trees) and jury found an answer (3 trees)

Test #3:

score: 0
Accepted
time: 0ms
memory: 3600kb

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: 3848kb

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: 0
Accepted
time: 1ms
memory: 3884kb

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:

60
3835 1 2 3 5 6 7 8 9 10 11 12 14 16 19 20 21 24 25 26 
749 1 2 3 4 5 6 7 8 9 10 11 12 14 16 19 20 21 25 26 
5288 1 2 3 4 6 7 8 9 10 11 12 14 16 19 20 21 24 25 26 
0 1 2 3 4 5 6 7 8 9 10 11 12 16 19 20 21 24 25 26 
4020 1 2 3 4 5 6 7 8 9 10 11 12 14 16 20 21 24 25 26 
8322 1 2 3 4 5 6 7 8 10 11 12...

result:

ok Participant found an answer (60 trees) and jury found an answer (59 trees)

Test #6:

score: 0
Accepted
time: 0ms
memory: 3752kb

input:

50 80 99991
6 5 67664
39 4 74944
11 9 13035
13 48 81979
40 20 57943
20 31 72081
1 6 39307
48 39 3550
28 48 41071
18 28 42935
37 32 7538
37 29 3815
50 37 88043
38 41 7283
40 26 66278
37 34 60696
47 19 80875
4 26 67
20 32 91858
39 24 83485
45 25 12241
48 46 61691
37 44 47541
39 40 70034
37 42 25006
27...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #7:

score: 0
Accepted
time: 18ms
memory: 3796kb

input:

100 150 999983
84 10 999545
69 48 930138
48 13 303468
36 6 668122
91 84 115623
62 71 59711
12 37 749281
86 49 281976
26 46 624831
91 8 450475
92 55 460900
50 63 513056
72 2 477622
26 96 11359
31 82 953946
6 71 406339
24 7 177090
70 4 67359
31 39 795565
47 32 407459
26 35 760698
22 37 508175
8 93 612...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #8:

score: 0
Accepted
time: 76ms
memory: 4112kb

input:

200 250 9999991
170 185 3242943
70 17 6083198
137 55 4000889
15 171 1113989
108 65 7988488
192 37 8812990
53 143 8707264
80 180 2504807
55 163 2706048
67 64 6210980
87 165 7693967
155 122 8550804
56 99 7228534
114 138 7047731
190 196 6684929
86 197 8866886
38 195 6717874
112 133 7257617
160 104 3210...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #9:

score: 0
Accepted
time: 974ms
memory: 8864kb

input:

500 600 99999989
265 416 47066772
354 266 16969437
195 415 7917612
354 136 43128175
163 191 58723996
144 84 65835385
157 45 94124747
232 441 17509499
70 397 64101208
223 387 7043647
320 47 84970673
100 2 87310855
87 131 75042257
101 391 27645446
79 26 68547739
390 185 92142961
257 15 80922292
276 48...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #10:

score: 0
Accepted
time: 1654ms
memory: 10960kb

input:

500 700 99999989
250 2 71289880
454 447 70661327
328 253 57519343
11 201 67456781
294 99 23392419
215 322 61059212
411 389 69899684
488 429 89579827
437 79 60564061
413 380 34922641
477 372 14858185
156 44 3101349
88 8 52225146
115 26 8582010
171 237 33206748
237 495 31192017
146 32 62712576
209 352...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #11:

score: 0
Accepted
time: 2483ms
memory: 13440kb

input:

500 800 99999989
258 304 1237432
159 152 6684056
8 47 64155938
436 265 83092505
204 302 3892712
142 302 77925167
37 15 20298972
202 395 35856655
284 260 96812598
365 172 48834835
196 101 64871741
174 45 37729972
302 206 90932677
305 275 27712443
443 157 81820535
16 248 22708463
461 479 64749118
105 ...

output:

-1

result:

ok Both jury and participant did not find an answer

Test #12:

score: -100
Time Limit Exceeded

input:

500 900 99999989
122 188 44796717
73 121 56798468
334 358 95823235
485 453 96779071
209 391 45946094
332 168 91056077
481 483 81268636
148 393 25213027
107 214 99281713
493 46 61525618
472 355 74320568
258 482 99615552
159 393 20311839
411 121 5207095
20 131 65269699
45 339 51772607
195 292 64556504...

output:


result: