QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#278613 | #6872. Magma Cave | anhduc2701 | WA | 8ms | 55200kb | C++23 | 3.9kb | 2023-12-07 18:28:01 | 2023-12-07 18:28:02 |
Judging History
answer
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,fma,bmi,bmi2,sse4.2,popcnt,lzcnt")
#include <bits/stdc++.h>
#define taskname ""
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define i64 long long
#define pb push_back
#define ff first
#define ss second
#define isz(x) (int)x.size()
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const int mod = 1e9 + 7;
const int blk=500;
const i64 oo = 1e18;
int n,q;
struct edge{
int u,v,w;
edge(){}
edge(int _u,int _v,int _w){
u=_u, v=_v, w=_w;
}
}canh[maxn];
vector<int>Ga[maxn];
vector<int>Gb[maxn];
int pa[maxn];
int ran[maxn];
struct dsu{
int u,v,ranu;
dsu(){}
dsu(int _u,int _v,int _ranu){
u=_u;
v=_v;
ranu=_ranu;
}
};
int fin(int u){
if(pa[u]==u)return u;
return fin(pa[u]);
}
stack<dsu>s;
void rollback(){
dsu p=s.top();
s.pop();
ran[p.u]-=p.ranu;
pa[p.v]=p.v;
}
bool join(int u,int v){
int x=fin(u);
int y=fin(v);
if(x==y)return false;
if(ran[x]<ran[y]){
swap(x,y);
}
s.push(dsu(x,y,ran[x]==ran[y]));
if(ran[x]==ran[y])ran[x]++;
pa[y]=x;
return true;
}
int mi[maxn];
int ma[maxn];
void solve() {
cin >> n >> q;
int last=0;
for(int i=1;i<=n;i++){
pa[i]=i;
ran[i]=0;
}
vector<int>poi;
vector<int>idx;
for(int i=1;i<=q;i++){
int type,u,v,w;
cin >> type;
if (type == 1){
cin >> u >> v >> w;
canh[i]=edge(u,v,w);
poi.pb(w);
}
else{
int k;
cin >> k >> w;
canh[i]=edge(k,0,w);
}
}
sort(poi.begin(),poi.end());
poi.resize(unique(poi.begin(),poi.end())-poi.begin());
for(int i=1;i<=q;i++){
if(canh[i].v!=0){
canh[i].w=lower_bound(poi.begin(),poi.end(),canh[i].w)-poi.begin();
idx.pb(i);
}
if(idx.size()==blk || i==q){
for(int j=last+1;j<=i;j++){
if(canh[j].v==0){
int x=lower_bound(poi.begin(),poi.end(),canh[i].w)-poi.begin();
Gb[x].pb(j);
}
}
for(int j=0;j<(int)poi.size();j++){
for(auto v:Ga[j]){
join(canh[v].u,canh[v].v);
}
for(auto v:Gb[j]){
int cnt=0;
for(auto j:idx){
if(j>v)break;
if(canh[j].w<=canh[v].w){
cnt+=join(canh[j].u,canh[j].v);
}
}
ma[v]=s.size();
for(int j=1;j<=cnt;j++){
rollback();
}
}
}
while(s.size()){
rollback();
}
for(int j=poi.size()-1;j>=0;j--){
for(auto v:Gb[j]){
int cnt=0;
for(auto j:idx){
if(j>v)break;
if(canh[j].w>canh[v].w){
cnt+=join(canh[j].u,canh[j].v);
}
}
mi[v]=n-1-s.size();
for(int j=1;j<=cnt;j++){
rollback();
}
}
for(auto v:Ga[j]){
if(v<=last && canh[v].v!=0){
join(canh[v].u,canh[v].v);
}
}
Gb[j].clear();
}
while(s.size()){
rollback();
}
for(int j=last+1;j<=i;j++){
if(canh[j].v!=0){
Ga[canh[j].w].pb(j);
}
}
last=i;
idx.clear();
}
}
for(int i=0;i<=poi.size();i++){
Ga[i].clear();
}
int cnt=n-1;
map<int,bool>xh;
for(int i=1;i<=q;i++){
if(canh[i].v!=0){
xh[poi[canh[i].w]]=1;
if(cnt!=0)cnt-=join(canh[i].u,canh[i].v);
}
else{
if(cnt==0 && xh.find(canh[i].w)!=xh.end() && mi[i]<=canh[i].u && canh[i].u<=ma[i] ){
cout << "YES" << "\n";
}
else{
cout << "NO" << "\n";
}
}
}
}
signed main() {
freopen("input.inp","r",stdin);
auto start = chrono::high_resolution_clock::now();
cin.tie(0),cout.tie(0)->sync_with_stdio(0);
int t = 1; cin >> t;
while(t--) {
solve();
}
auto end = chrono::high_resolution_clock::now();
cout << "\n"; for(int i = 1; i <= 100; ++i) cout << '=';
cout << "\nExecution time: " << chrono::duration_cast<chrono::milliseconds> (end - start).count() << "[ms]" << endl;
}
详细
Test #1:
score: 0
Wrong Answer
time: 8ms
memory: 55200kb
input:
100 500 1999 1 112 419 318 1 419 208 1700 1 208 255 603 1 255 202 306 1 202 54 326 1 54 468 1506 1 468 23 856 1 23 90 758 1 90 271 1104 1 271 442 1900 1 442 441 19 1 441 378 1227 1 378 466 24 1 466 186 228 1 186 399 1387 1 399 288 297 1 288 144 1763 1 144 418 1896 1 418 217 673 1 217 281 1259 1 281 ...
output:
==================================================================================================== Execution time: 0[ms]
result:
wrong answer 1st lines differ - expected: 'NO', found: ''