結果

問題 No.2523 Trick Flower
ユーザー RubikunRubikun
提出日時 2023-10-27 22:33:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 347 ms / 4,000 ms
コード長 2,874 bytes
コンパイル時間 2,418 ms
コンパイル使用メモリ 215,784 KB
実行使用メモリ 20,244 KB
最終ジャッジ日時 2023-10-27 22:33:53
合計ジャッジ時間 8,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,824 KB
testcase_01 AC 3 ms
6,824 KB
testcase_02 AC 3 ms
6,824 KB
testcase_03 AC 3 ms
6,780 KB
testcase_04 AC 3 ms
6,780 KB
testcase_05 AC 3 ms
6,824 KB
testcase_06 AC 3 ms
6,824 KB
testcase_07 AC 3 ms
6,828 KB
testcase_08 AC 5 ms
6,892 KB
testcase_09 AC 4 ms
6,880 KB
testcase_10 AC 4 ms
6,844 KB
testcase_11 AC 4 ms
6,852 KB
testcase_12 AC 3 ms
6,848 KB
testcase_13 AC 4 ms
6,880 KB
testcase_14 AC 4 ms
6,876 KB
testcase_15 AC 347 ms
20,244 KB
testcase_16 AC 308 ms
20,244 KB
testcase_17 AC 87 ms
12,068 KB
testcase_18 AC 75 ms
11,004 KB
testcase_19 AC 321 ms
13,116 KB
testcase_20 AC 322 ms
13,116 KB
testcase_21 AC 325 ms
13,116 KB
testcase_22 AC 303 ms
13,116 KB
testcase_23 AC 330 ms
13,116 KB
testcase_24 AC 166 ms
10,308 KB
testcase_25 AC 272 ms
12,324 KB
testcase_26 AC 260 ms
12,060 KB
testcase_27 AC 299 ms
12,588 KB
testcase_28 AC 228 ms
11,796 KB
testcase_29 AC 48 ms
7,836 KB
testcase_30 AC 202 ms
11,004 KB
testcase_31 AC 12 ms
7,156 KB
testcase_32 AC 123 ms
9,204 KB
testcase_33 AC 305 ms
12,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=998244353,MAX=100005,INF=1<<30;

//Union Find


struct UF{
    int n;
    vector<int> par,size,edge;
    
    void init(int n_){
        n=n_;
        par.assign(n,-1);
        size.assign(n,1);
        edge.assign(n,0);
        
        for(int i=0;i<n;i++){
            par[i]=i;
        }
    }
    
    int root(int a){
        if(par[a]==a) return a;
        else return par[a]=root(par[a]);
    }
    
    void unite(int a,int b){
        edge[root(a)]++;
        if(root(a)!=root(b)){
            size[root(a)]+=size[root(b)];
            edge[root(a)]+=edge[root(b)];
            par[root(b)]=root(a);
        }
    }
    
    bool check(int a,int b){
        return root(a)==root(b);
    }
};

vector<int> G[MAX];

ll X[MAX],Y[MAX];

void solve(int u){
    for(int to:G[u]){
        solve(to);
        X[u]-=(Y[to]-X[to]);
        chmax(X[u],-(1LL<<60));
    }
    chmin(X[u],Y[u]);
}

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int N;cin>>N;
    vector<ll> A(N),B(N),C(N);
    vector<int> deg(N);
    UF uf;uf.init(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    for(int i=0;i<N;i++){
        cin>>B[i];
    }
    for(int i=0;i<N;i++){
        cin>>C[i];C[i]--;
        deg[C[i]]++;
    }
    
    queue<int> Q;
    for(int i=0;i<N;i++){
        if(deg[i]==0) Q.push(i);
    }
    
    while(!Q.empty()){
        int u=Q.front();Q.pop();
        G[C[u]].push_back(u);
        deg[C[u]]--;
        if(deg[C[u]]==0) Q.push(C[u]);
    }
    
    for(int i=0;i<N;i++){
        if(deg[i]){
            uf.unite(C[i],i);
        }
    }
    
    for(int i=0;i<N;i++){
        if(deg[i]){
            if(uf.root(i)!=i){
                int ro=uf.root(i);
                A[ro]+=A[i];
                B[ro]+=B[i];
                for(int x:G[i]) G[ro].push_back(x);
                G[i].clear();
                A[i]=0;
                B[i]=0;
            }
        }
    }
    
    ll left=0,right=1LL<<60;
    while(right-left>1){
        ll mid=(left+right)/2;
        for(int i=0;i<N;i++){
            X[i]=A[i];
            if(B[i]<=(1LL<<60)/mid) Y[i]=B[i]*mid;
            else Y[i]=1LL<<60;
        }
        bool ok=true;
        for(int i=0;i<N;i++){
            if(deg[i]&&uf.root(i)==i){
                solve(i);
                ok&=X[i]>=Y[i];
            }
        }
        
        if(ok) left=mid;
        else right=mid;
    }
    cout<<left<<endl;
}
0