結果

問題 No.1773 Love Triangle
ユーザー kazu0x17
提出日時 2022-08-18 14:47:05
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
RE  
実行時間 -
コード長 4,701 bytes
コンパイル時間 1,211 ms
コンパイル使用メモリ 111,264 KB
最終ジャッジ日時 2025-01-30 23:50:54
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 RE * 1
other AC * 20 WA * 3 RE * 58 TLE * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<random>
#include<vector>
#include<chrono>
#include<tuple>
#include<numeric>
using namespace std;
using lli = long long int;

class rational{
public:
    rational(){x = 0, y = 1;}
    rational(lli a, lli b){x = a, y = b;}
    lli x, y;
    rational operator+(rational a){
        rational r((x*a.y + a.x*y), y*a.y);
        lli g = gcd(r.x, r.y);
        r.x /= g, r.y /= g;
        if(r.y < 0) r.x = -r.x, r.y = -r.y;
        return r;
    }
    rational operator-(rational a){
        rational r((x*a.y - a.x*y), y*a.y);
        lli g = gcd(r.x, r.y);
        r.x /= g, r.y /= g;
        if(r.y < 0) r.x = -r.x, r.y = -r.y;
        return r;
    }
    rational operator*(rational a){
        rational r(x*a.x, y*a.y);
        lli g = gcd(r.x, r.y);
        r.x /= g, r.y /= g;
        if(r.y < 0) r.x = -r.x, r.y = -r.y;
        return r;
    }
    rational operator/(rational a){
        if(a.x == 0){
            cerr << "divided by zero." << endl;
            exit(1);
        }
        rational r(x*a.y, y*a.x);
        lli g = gcd(r.x, r.y);
        r.x /= g, r.y /= g;
        if(r.y < 0) r.x = -r.x, r.y = -r.y;
        return r;
    }
};

mt19937 mt(234298343);
// uniform_int_distribution<lli> rndgen(0, 49725516559);
uniform_int_distribution<lli> rndgen(0, 1e9+7);

int Rank(vector<vector<rational> > M){
    // for(int i = 0; i < M.size(); i++)  {
    //     for(int j = 0; j < M.size(); j++) {
    //         cout << M[i][j].x << "/" << M[i][j].y << " ";
    //     }
    //     cout << endl;
    // }
    // cout << endl;
    // cout << endl;

    int pivot = -1, r = 0;
    rational z;
    for(int i = 0; i < M.size(); i++) {
        // cout << "i:" << i  << ", pivot:" << pivot << ", n:" << M.size() << endl;
        for(int j = pivot + 1; j < M.size(); j++) {
            // cout << "j:" << j << endl;            
            pivot = 1e9;
            if(M[i][j].x != 0)pivot = j;
            for(int k = i+1; k < M.size() and M[i][j].x == 0; k++) {
                // cout << "hoge:" << M[i][j].x << endl;
                // cout << "fuga:" << M[k][j].x << endl;
                if(M[i][j].x == 0 and M[k][j].x != 0){
                    swap(M[i], M[k]);
                    // z = rational(M[i][j]);
                    pivot = j;
                    break;
                }
            }
            if(pivot == j) break;
        }
        if(pivot >= M.size() - 1) break; 
        z = M[i][pivot];

        // for(int i = 0; i < M.size(); i++)  {
        //     for(int j = 0; j < M.size(); j++) {
        //         cout << M[i][j].x << "/" << M[i][j].y << " ";
        //     }
        //     cout << endl;
        // }
        // cout << "piv:" << pivot << ", i:" << i << ", n:" << M.size() << endl;
        // cout << "z:" << z.x << "/" << z.y << endl;
        for(int j = 0; j < M.size(); j++) M[i][j] = M[i][j] / z;
        for(int j = i+1; j < M.size(); j++) {
            rational w = M[j][pivot];
            // cout << "wwww:" << w.x << "/" << w.y << endl;
            for(int k = 0; k < M.size(); k++)  M[j][k] = M[j][k] - (w * M[i][k]);
        }
        // cout << endl;
        // cout << endl;
    }
    r = 1e9;
    for(int i = M.size() - 1; i >= 0 and r == 1e9; i--)  {
        for(int j = 0; j < M.size() and r == 1e9; j++) {
            if(M[i][j].x != 0) r = i;
        }
    }
    // for(int i = 0; i < M.size(); i++)  {
    //     for(int j = 0; j < M.size(); j++) {
    //         if(M[i][j].x != 0) cout << "1 ";
    //         else cout << "0 ";
    //     }
    //     // for(int j = 0; j < M.size(); j++) {
    //     //     cout << M[i][j].x << "/" << M[i][j].y << " ";
    //     // }
    //     cout << endl;
    // }
    return r;
}

int main(){
    int n, m;
    // lli mod = 49725516559;
    lli mod = 1e9+7;
    cin >> n >> m;
    vector<vector<lli> > M(n, vector<lli>(n, 0));
    for(int i = 0; i < m; i++) {
        int x = rndgen(mt);
        int u, v, w;
        cin >> u >> v >> w;
        u--, v--, w--;
        M[u][v] = (M[u][v] + x)%mod;
        M[v][w] = (M[v][w] + x)%mod;
        M[w][u] = (M[w][u] + x)%mod;

        M[v][u] = (M[v][u] - x + mod)%mod;
        M[w][v] = (M[w][v] - x + mod)%mod;
        M[u][w] = (M[u][w] - x + mod)%mod;
    }

    vector<vector<rational> > ratM(n, vector<rational>(n));
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            ratM[i][j] = rational(M[i][j], 1);
        }
    }
    int ans = 0;
    for(int i = 0; i < 10; i++) ans = max(ans, Rank(ratM));
    // for(int i = 0; i < n; i++) {
    //     for(int j = 0; j < n; j++) {
    //         ratM[i][j] = rational(1, 1);
    //     }
    // }
    // Rank(ratM);
    cout << ans/2 << endl;
}
0