QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#546839 | #852. Jellyfish | Rafat_Kabir | AC ✓ | 196ms | 20588kb | C++20 | 5.4kb | 2024-09-04 14:34:13 | 2024-09-04 14:34:16 |
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
// send empty cycle array to the functions
bool hasCycleDFS(int node, vector<vector<int>>& adjList, vector<int>& state, vector<int>& parent, vector<int>& cycle) {
state[node] = 1; // Visited
for (int neighbor : adjList[node]) {
if (state[neighbor] == 0) {
// Not visited, continue DFS
parent[neighbor] = node;
if (hasCycleDFS(neighbor, adjList, state, parent, cycle)) {
return true;
}
} else if (state[neighbor] == 1 && neighbor != parent[node]) {
// Visited and in recursion stack, a cycle is found
int current = node;
while (current != neighbor) {
cycle.push_back(current);
current = parent[current];
}
cycle.push_back(neighbor);
// cycle.push_back(node); // Close the cycle
return true;
}
}
state[node] = 2; // Marked as processed
return false;
}
bool hasCycle(int numVertices, vector<vector<int>>& adjList, vector<int>& cycle) {
vector<int> state(numVertices, 0); // 0: Not visited, 1: Visited, 2: Processed
vector<int> parent(numVertices, -1);
for (int i = 0; i < numVertices; ++i) {
if (state[i] == 0 && hasCycleDFS(i, adjList, state, parent, cycle)) {
return true;
}
}
return false;
}
void solve(int it)
{
ll n;
cin >> n;
vv32 adj(n);
FOR(0, n -1, i){
ll u, v; cin >> u >>v;
--u; --v;
adj[u].pb(v);
adj[v].pb(u);
}
v32 cycle;
hasCycle(n, adj, cycle);
vb vis(n, false);
for(auto v : cycle){
vis[v] = true;
}
v32 sub(n);
v32 A;
ll ans = 3;
ll sum = 0;
auto dfs = [&](auto&& self, ll u, ll p = -1)->void{
vis[u] = true;
bool ok = true;
for(auto v : adj[u]){
if(vis[v]) continue;
if(v == p) continue;
ok = false;
self(self, v, u);
sub[u] += sub[v];
}
if(ok && adj[u].size() == 1){
// cout << u + 1 << "->\n";
sub[u] = 1;
}
// A.pb(sub[u]);
// sum += sub[u];
};
for(auto x : cycle){
// cout << x + 1 << " ";
dfs(dfs, x);
}
// cout << '\n';
for(auto x : cycle) A.pb(sub[x]), sum += sub[x];
// cout << sum << " ";
ans = max(ans, sum);
FOR(0, cycle.size() - 1, i){
int nxt = (i + 1) % (int)cycle.size();
ans = max({ans, sum - A[i] - A[nxt] + 2, sum - A[i] + 1});
}
cout << ans << "\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: 3616kb
input:
2 6 1 2 2 3 3 4 4 1 2 5 2 6 4 1 2 2 3 3 4 4 1
output:
4 3
result:
ok 2 number(s): "4 3"
Test #2:
score: 0
Accepted
time: 180ms
memory: 3768kb
input:
85665 6 3 2 4 1 4 6 2 1 2 6 5 1 7 6 2 6 3 5 1 1 2 2 4 4 7 3 7 7 6 1 6 7 1 4 1 3 7 5 5 3 4 2 7 6 2 7 4 7 5 3 1 3 4 2 5 1 4 7 7 2 2 6 5 4 5 6 5 1 3 1 4 6 7 3 5 3 1 3 7 3 2 5 1 5 4 4 6 7 4 5 4 1 3 6 3 7 6 7 6 1 2 1 7 5 3 7 3 1 4 6 2 6 3 2 3 4 3 7 2 3 2 6 2 4 7 5 3 5 5 1 1 4 7 3 4 3 7 5 6 2 7 4 6 6 7 6 ...
output:
4 3 3 3 3 4 4 5 4 5 4 4 3 4 4 3 4 4 4 4 4 5 3 4 3 4 3 9 4 4 3 4 8 3 98 5 4 3 6 4 4 4 4 3 4 4 4 4 5 3 5 4 3 4 95 4 4 4 5 4 3 4 3 5 4 3 4 3 3 4 4 4 4 4 3 4 4 4 3 3 3 4 4 3 4 4 4 4 4 4 3 3 5 5 4 5 4 3 4 4 3 3 3 5 4 4 4 6 4 5 5 5 4 3 5 4 4 3 4 10 4 3 3 4 4 3 5 4 4 3 5 3 4 4 3 3 3 4 5 98 5 105 4 4 4 3 4 ...
result:
ok 85665 numbers
Test #3:
score: 0
Accepted
time: 196ms
memory: 20588kb
input:
30 2229 1066 248 248 881 881 2080 2080 1615 1615 1647 1647 1774 1774 196 196 434 434 390 390 129 129 563 563 63 63 1457 1457 1015 1015 2200 2200 1187 1187 1763 1763 1121 1121 2122 2122 1783 1783 1756 1756 2031 2031 2153 2153 605 605 1778 1778 1287 1287 2062 2062 817 817 194 194 474 474 414 414 1736 ...
output:
1092 881 722 1412 37556 638 438 509 273 29198 740 27535 46011 865 444 30031 49564 794 489 469 624 956 1180 17384 50000 715 1291 49920 1465 3
result:
ok 30 numbers