QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#553099 | #853. Flat Organization | Rafat_Kabir | RE | 0ms | 3780kb | C++20 | 6.6kb | 2024-09-08 08:36:49 | 2024-09-08 08:36:49 |
Judging History
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-1;
// 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);
}
}
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;
}
intervals.pb({mx-1, mx-1, 0, -1, -1});
n = intervals.size();
intervals.pb({0, 0, 0, -1, -1});
sort(all(intervals), [&](e a, e b){
return a.start < b.start;
});
// cout << "sorting done\n";
v32 par(n,-1);
v64 dp(n, 0);
set<pair<ll, p64>>s1;
set<p64>s2;
s1.insert({0, {0, -1}});
s2.insert({0, -1});
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();
dp[i] = intervals[i].wt + mn.fi;
par[i] = mn.second;
s1.insert({intervals[i].end, {intervals[i].wt, i}});
s2.insert({intervals[i].wt, i});
}
// cout << "dp done\n";
vp64 ans;
int cur = n-1;
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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
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
Runtime Error
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 ...