QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#553152#853. Flat OrganizationRafat_KabirWA 69ms3888kbC++207.9kb2024-09-08 09:59:162024-09-08 09:59:16

Judging History

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

  • [2024-09-08 09:59:16]
  • 评测
  • 测评结果:WA
  • 用时:69ms
  • 内存:3888kb
  • [2024-09-08 09:59:16]
  • 提交

answer

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <time.h>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <cstring>

using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>

using namespace __gnu_pbds;
using namespace std;
template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// to erase in multiset-> less_equal<T> and 
// s.erase(s.find_by_order(s.order_of_key(x)))
// lower_bound(x)=>(cannot use the stl lower_bound function)
// ll idx = s.order_of_key(x)
// if(idx == s.size()) -> no lower_bound
// else lb = *s.find_by_order(idx) // as 0-indexing
// idx-1 will give highest value which is strictly less than x
// for upper_bound->do the same with (x+1)

typedef long long ll;
typedef long double ld;
typedef pair<int,int> p32;
typedef pair<ll,ll> p64;
typedef tuple<ll, ll, ll> t64;
typedef vector<t64> vt64;
typedef vector<vt64> vvt64;
typedef pair<double,double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
typedef vector<vector<p32> > vvp32;
typedef vector<bool> vb;
ll mod =  1e9+7, MOD = 998244353;
double eps = 1e-12;
// #define forn(i,e) for(ll i = 0; i < e; i++)
#define FOR(s, e, i) for(int i = s; i <= e; i++)
// #define rforn(i,s) for(ll i = s; i >= 0; i--)
#define ROF(s ,e, i) for(int i = s; i >= e; i--)
#define coutAll(A) for(auto asdafas : A) cout <<  asdafas << " "; cout << "\n";
#define foutAll(A) for(auto asdafas : A) fout <<  asdafas << " "; cout << "\n";
#define cinAll(A) for(auto &asdafas : A) cin >>  asdafas;
#define finAll(A) for(auto &asdafas : A) fin >>  asdafas;
#define minpq priority_queue<ll, v64, greater<ll>>
#define maxpq priority_queue<ll> 
#define ln "\n"
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define fi first
#define se second
ll inf = LLONG_MAX;
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((ll)(x).size())
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<ll, ll> pll;
typedef pair<ll, ll> pii;
//#define MAXN 1000000

struct e{
    ll start, end, wt, a, b;
};

vector<bool> visited; // keeps track of which vertices are already visited

// runs depth first search starting at vertex v.
// each visited vertex is appended to the output vector when dfs leaves it.
void dfs(int v, vector<vector<p64>> const& adj, vector<int> &output) {
    visited[v] = true;
    for (auto u : adj[v])
        if (!visited[u.fi])
            dfs(u.fi, adj, output);
    output.push_back(v);
}
int mx;
// input: adj -- adjacency list of G
// output: components -- the strongy connected components in G
// output: adj_cond -- adjacency list of G^SCC (by root vertices)
void strongy_connected_components(vector<vector<p64>> const& adj,
                                  vector<vector<int>> &components,
                                  vector<e> &adj_cond) {
    int n = adj.size();
    components.clear(), adj_cond.clear();

    vector<int> order; // will be a sorted list of G's vertices by exit time

    visited.assign(n, false);

    // first series of depth first searches
    for (int i = 0; i < n; i++)
        if (!visited[i])
            dfs(i, adj, order);

    // create adjacency list of G^T
    vector<vector<p64>> adj_rev(n);
    for (int v = 0; v < n; v++)
        for (auto u : adj[v])
            adj_rev[u.fi].push_back({v, u.se});

    visited.assign(n, false);
    reverse(order.begin(), order.end());

    vector<int> roots(n, 0); // gives the root vertex of a vertex's SCC
    v32 idx(n, -1);
    int id = 0;
    // second series of depth first searches
    for (auto v : order)
        if (!visited[v]) {
            std::vector<int> component;
            dfs(v, adj_rev, component);
            sort(component.begin(), component.end());
            components.push_back(component);
            int root = component.front();
            idx[root] = id++;
            for (auto u : component)
                roots[u] = root;
        }
    mx = id;
    // for(auto c : components){
    //     FOR(0, c.size() - 1, i) cout << c[i]+1 << " ";
    //     cout << "\n";
    // }
    // cout << "idx => ";
    // coutAll(idx);
    // add edges to condensation graph
    // adj_cond.assign(id, {});
    for (int v = 0; v < n; v++)
        for (auto u : adj[v])
            if (roots[v] != roots[u.fi])
                adj_cond.pb({idx[roots[v]], idx[roots[u.fi]], u.se, v, u.fi});
}
void solve(int it)
{
    ll n; cin >> n;
    vvp64 adj(n);
    FOR(0, n  -1 ,i){
        FOR(0, n - 1, j){
            ll d; cin >> d;
            if(d == 0) continue;
            adj[i].emplace_back(j, d);
        }
    }
    // FOR(0, n - 1, i){
    //     cout << i+1 << "-> ";
    //     for(auto x : adj[i]) cout << x.fi+1 << " ";
    //     cout << "\n";
    // }
    vv32 components;
    vector<e>intervals;
    strongy_connected_components(adj, components, intervals);
    // cout << "strongy_connected_components done\n";
    // if(intervals.size() <= 1){
    //     cout << "NO\n";
    //     return;
    // }
    // cout << mx << "\n";
    if(mx <= 1){
        cout << "YES\n0 0\n";
        return;
    }
    if(mx == 2){
        // cout << "interval size = " << intervals.size() << "\n";
        // for(auto x : intervals){
        //     cout << x.a << "->" << x.b << "\n";
        // }
        if(intervals.size() <= 1){
            cout << "NO\n";
            return;
        }
        sort(all(intervals), [&](e a, e b){
            return a.wt < b.wt;
        });
        cout << "YES\n1 " << intervals[0].wt << "\n" << intervals[0].a+1
        << " " << intervals[0].b+1 << "\n";
        return;
    }
    intervals.pb({mx-1, mx-1, 0, -1, -1});
    intervals.pb({0, 0, 0, -1, -1});
    sort(all(intervals), [&](e a, e b){
        if(a.start == b.start) return a.end < b.end;
        return a.start < b.start;
    });
    n = intervals.size();
    // cout << "mx = " << mx << "\n";
    // for(auto x : intervals) cout << x.start << "->" << x.end << " " << x.wt << "\n";
    // cout << "sorting done\n";
    v32 par(n,-1);
    v64 dp(mx, inf);
    dp[0] = 0;
    set<pair<ll, p64>>s1;
    set<p64>s2;
    s1.insert({0, {0, -1}});
    s2.insert({0, -1});
    ll idd = 0;
    FOR(0, n - 1, i){
        while((*s1.begin()).fi < intervals[i].start){
            auto x = *s1.begin();
            s2.erase(s2.find(x.se));
            s1.erase(s1.begin());
        }
        auto mn = *s2.begin();
        if(dp[intervals[i].end] > intervals[i].wt + mn.fi){
            dp[intervals[i].end] = intervals[i].wt + mn.fi;
            par[i] = mn.se;
            idd = i;
        }
        s1.insert({intervals[i].end, {dp[intervals[i].end], i}});
        s2.insert({dp[intervals[i].end], i});
    }
    // cout << "dp  = ";
    // coutAll(dp);
    // cout << "dp done\n";
    vp64 ans;
    int cur = idd;
    while(cur != -1){
        if(intervals[cur].a != -1)
            ans.emplace_back(intervals[cur].a, intervals[cur].b);
        cur = par[cur];
    }
    cout << "YES\n";
    cout << ans.size() << " " << dp.back() << "\n";
    FOR(0, ans.size()-1, i){
        cout << ans[i].fi+1 << " " << ans[i].se+1 << "\n";
    }


}


int main()
{
    // fast_cin();    
    ll t = 1;
    cin >> t;
    for(int it=1; it<=t; it++)
    {
        //cout << "Case " << it << ": ";
        solve(it);
    }
    return 0;
}


Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

1
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0

output:

YES
2 10
4 5
1 4

result:

ok OK!

Test #2:

score: -100
Wrong Answer
time: 69ms
memory: 3888kb

input:

100
5
0 1 0 6 11
0 0 1 6 12
2 0 0 7 12
0 0 0 0 4
0 0 0 0 0
1
0
2
0 0
7 0
2
0 1000000000
0 0
3
0 0 5
6 0 0
0 7 0
3
0 4 6
0 0 0
0 1 0
3
0 4 0
0 0 0
6 3 0
3
0 0 0
10 0 6
3 0 0
3
0 1999999998 1999999999
0 0 1999999998
0 0 0
50
0 105800 801 1800 64800 0 259200 288801 72201 12801 125000 20001 28801 33800 ...

output:

YES
2 10
4 5
1 4
YES
0 0
NO
NO
YES
0 0
YES
1 4
1 2
YES
1 3
3 2
YES
2 9
3 1
2 3
YES
1 1999999999
1 3
YES
3 602
34 39
11 47
4 33
YES
3 649
17 29
32 45
27 12
YES
5 1209
25 12
4 25
35 4
14 35
1 18
YES
3 844
14 21
1 41
3 8
YES
3 866
46 26
35 44
17 35
YES
4 1066
37 8
24 2
36 24
10 28
YES
3 1122
5 22
43 19...

result:

wrong answer Test 51: Sum of weights of reversed edges is different than declared