結果

問題 No.1950 片道きゃっちぼーる
ユーザー woodywoodywoodywoody
提出日時 2022-06-06 01:15:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 467 ms / 3,000 ms
コード長 6,183 bytes
コンパイル時間 5,342 ms
コンパイル使用メモリ 258,364 KB
実行使用メモリ 110,752 KB
最終ジャッジ日時 2023-10-21 03:24:14
合計ジャッジ時間 15,805 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 455 ms
110,752 KB
testcase_04 AC 467 ms
79,500 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 198 ms
67,836 KB
testcase_07 AC 253 ms
49,124 KB
testcase_08 AC 337 ms
49,124 KB
testcase_09 AC 302 ms
50,080 KB
testcase_10 AC 441 ms
74,844 KB
testcase_11 AC 458 ms
74,832 KB
testcase_12 AC 456 ms
74,836 KB
testcase_13 AC 394 ms
73,224 KB
testcase_14 AC 216 ms
35,628 KB
testcase_15 AC 374 ms
55,172 KB
testcase_16 AC 208 ms
51,996 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 387 ms
64,700 KB
testcase_19 AC 361 ms
52,496 KB
testcase_20 AC 306 ms
49,124 KB
testcase_21 AC 253 ms
39,588 KB
testcase_22 AC 262 ms
36,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
#define rep(i,b) for(int i=0;i<b;i++)
#define rrep(i,b) for(int i=b-1;i>=0;i--)
#define rep1(i,b) for(int i=1;i<b;i++)
#define repx(i,x,b) for(int i=x;i<b;i++)
#define fore(i,a) for(auto i:a)
#define fore1(i,a) for(auto &i:a)
#define rng(x) (x).begin(), (x).end()
#define rrng(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define fi first
#define se second

using namespace std;
using namespace atcoder;

using ll = long long;
template<typename T> using mpq = priority_queue<T, vector<T>, greater<T>>;
template<typename T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<typename T> bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T> ll sumv(const vector<T>&a){ll res(0);for(auto&&x:a)res+=x;return res;}
bool yn(bool a) { if(a) {cout << "Yes" << endl; return 1;} else {cout << "No" << endl; return 0;}}
#define dame { cout << "No" << endl; return 0;}
#define dame1 { cout << -1 << endl; return 0;}
#define test(x) cout << "test" << x << endl;
#define deb(x,y) cout << x << " " << y << endl;
#define deb3(x,y,z) cout << x << " " << y << " " << z << endl;
#define deb4(x,y,z,x2) cout << x << " " << y << " " << z << " " << x2 << endl;
#define out cout << ans << endl;
#define outv fore(yans , ans) cout << yans << "\n";
#define show(x) cerr<<#x<<" = "<<x<<endl;
#define showv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;}
#define showv2(v) fore(vy , v) cout << vy << "\n";
using pll = pair<ll,ll>;
using pil = pair<int,ll>;
using pli = pair<ll,int>;
using pii = pair<int,int>;
using tp = tuple<int ,int ,int>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vpii = vector<pii>;
using vpli = vector<pli>;
using vpll = vector<pll>;
using vpil = vector<pil>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvs = vector<vector<string>>;
using vvb = vector<vector<bool>>;
using vvpii = vector<vector<pii>>;
using vvpli = vector<vector<pli>>;
using vvpll = vector<vpll>;
using vvpil = vector<vpil>;
using mint = modint998244353;
using vm = vector<mint>;
using vvm = vector<vector<mint>>;
vector<int> dx={1,0,-1,0,1,1,-1,-1},dy={0,1,0,-1,1,-1,1,-1};
ll gcd(ll a, ll b) { return a?gcd(b%a,a):b;}
ll lcm(ll a, ll b) { return a/gcd(a,b)*b;}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

struct scc{
    // iは強連結成分番号、jは頂点番号。
    vvi e,v; // e:有向辺、v:sccリスト(ACL) 
    int n,m; // n:頂点数、m:強連結成分数
    vi from_cnt,to_cnt; // from_cnt[i]:iから到達可能な頂点数、to_cnt[i]:iに到達可能な頂点数。
    vi sz; // sz[i]:強連結成分iに含まれる頂点数。
    vi led; // led[j]:頂点jが属する強連結成分番号
    vvi scc_g,scc_g_rev; // scc_g[i]:強連結成分を1つの頂点と見做したときの有向辺、scc_g_revは逆辺

    vi from_deg,to_deg; // from_deg[i]:強連結成分iから出る辺の数、to_deg[i]:入る辺の数

    vvb reach; // reach[i][i2]:iからi2に到達可能なときtrue、そうでないときfalse。

    scc(int n_) : n(n_) {
        e.resize(n);
        led.resize(n);
    }

    void add_edge(int a,int b){
        e[a].pb(b);
        return;
    }

    void init(){
        scc_graph g(n);
        rep(i,n) fore(y , e[i]) g.add_edge(i,y);
        v = g.scc();

        m=0;
        fore(y , v){
            fore(yy , y) led[yy] = m;   
            m++;
        }
        scc_g.resize(m);
        scc_g_rev.resize(m);
        from_deg.resize(m);
        to_deg.resize(m);
        from_cnt.resize(m);
        to_cnt.resize(m);
        sz.resize(m);

        fore(y,v) sz[led[y[0]]] = sz(y);

        map<pii , int> mp;
        rep(i,n) fore(y , e[i]){
            if (led[i] == led[y]) continue;
            if (mp.find({led[i],led[y]}) != mp.end()) continue;

            scc_g[led[i]].pb(led[y]);
            scc_g_rev[led[y]].pb(led[i]);
            from_deg[led[i]]++;
            to_deg[led[y]]++;
            mp[{led[i],led[y]}] = 0;
        }
        return;
    }


    // 計算量はO(n^2)
    void cnt_init(){
        reach.resize(m,vb(m,false));
        rep(i,m){
            reach[i][i] = true;
            queue<int> q;
            q.push(i);

            while(!q.empty()){
                int p = q.front();
                q.pop();

                fore(y , scc_g[p]){
                    if (reach[i][y]) continue;
                    reach[i][y] = true;
                    q.push(y);
                }
            }

            rep(j,m){
                if(reach[i][j]){
                    to_cnt[j] += sz[i];
                    from_cnt[i] += sz[j];
                }
            }
            
        }
        return;
    }
};

// scc(n) : 初期化。n頂点。
// add_edge(i , j) : 頂点iから頂点jへ辺を追加。
// init() : scc実行。強連結成分を1つの頂点と見做したときのグラフ(scc_g)を作成。計算量はO(n+m)。mは辺数。
// cnt_init() : 任意の強連結成分同士の到達判定。任意の頂点への(から)到達可能な頂点の数を計算。計算量はO(n^2)。

int main(){
    int n; cin>>n;

    scc s(n);

    unordered_map<ll , int> mp;

    vl x(n),a(n);

    rep(i,n){
        cin>>x[i];
        mp[x[i]] = i;
    }

    rep(i,n){
        cin>>a[i];
        if (mp.count(x[i] - a[i])){
            s.add_edge(i , mp[x[i] - a[i]]);
        }

        if (mp.count(x[i] + a[i])){
            s.add_edge(i , mp[x[i] + a[i]]);
        }
    }

    s.init();

    vl ans(n);

    queue<int> q;

    int l =  sz(s.v);

    rep(i,l){
        if (s.from_deg[i] == 0) q.push(i);
    }

    vl now(l);

    while(!q.empty()){
        int p = q.front();
        q.pop();
        
        rep(j,sz(s.v[p])) chmax(now[p] , x[s.v[p][j]] + a[s.v[p][j]]);

        fore(y , s.scc_g_rev[p]){
            chmax(now[y] , now[p]);
            s.from_deg[y]--;
            if (s.from_deg[y] == 0) q.push(y);
        }
    }

    rep(i,n) ans[i] = now[s.led[i]] - x[i];
    
    outv;

    return 0;
}
0