CSP-S 2022 考场代码
hukk/HN-S00154
用于博客:CSP-S 2022 游记
T1 holiday
#include<bits/stdc++.h>
using namespace std;//R
const int MXN=2510,MXM=10010;
int d[MXN][MXN];
unsigned long long val[MXN];
int n,m,k;
void floyd(){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
}
int main(){
// #ifndef LOCAL
freopen("holiday.in","r",stdin);
freopen("holiday.out","w",stdout);
// #endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>k;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
d[i][j]=0x3f3f3f3f;
}
d[i][i]=0;
}
for(int i=2;i<=n;i++){
cin>>val[i];
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
d[x][y]=d[y][x]=1;
}
floyd();
unsigned long long ans=0;k++;
for(int i=2;i<=n;i++){
if(d[1][i]>k) continue;
for(int j=2;j<=n;j++){
if(i==j||d[i][j]>k) continue;
for(int x=2;x<=n;x++){
if(i==x||j==x||d[j][x]>k) continue;
for(int y=2;y<=n;y++){
if(i==y||j==y||x==y) continue;
if(d[x][y]<=k&&d[y][1]<=k){
ans=max(ans,val[i]+val[j]+val[x]+val[y]);
}
}
}
}
}
cout<<ans<<endl;
return 0;
}T2 game
#include<bits/stdc++.h>
using namespace std;//P
const int MX=1e5+10;
struct node{
long long mx,mn;
long long mnp,mxi;
bool hz;//是否有0
node(long long a=LLONG_MIN,long long b=LLONG_MAX,long long _mnp=LLONG_MAX,long long _mxi=LLONG_MIN,bool c=0):mx(a),mn(b),hz(c){
mnp=_mnp;mxi=_mxi;
}
node operator + (const node &b){
return node(max(mx,b.mx),min(mn,b.mn),min(mnp,b.mnp),max(mxi,b.mxi),hz||b.hz);
}
};
struct SGT{
node tree[MX*4];
int n;
#define ls (t<<1)
#define rs (t<<1|1)
#define mid ((l+r)/2)
void pushup(int t){tree[t]=tree[ls]+tree[rs];}
void build(const long long a[],int l,int r,int t){
if(l==r){
tree[t]=node(a[l],a[l],(a[l]>0?a[l]:LLONG_MAX),(a[l]<0?a[l]:LLONG_MIN),a[l]==0);
return;
}
build(a,l,mid,ls);
build(a,mid+1,r,rs);
pushup(t);
}
node ask(int L,int R,int l,int r,int t){
if(L<=l&&r<=R){
return tree[t];
}
node ans;
if(L<=mid) ans=ans+ask(L,R,l,mid,ls);
if(R>mid) ans=ans+ask(L,R,mid+1,r,rs);
return ans;
}
node ask(int L,int R){
return ask(L,R,1,n,1);
}
SGT(const long long a[],const int _n){
n=_n;
build(a,1,n,1);
}
};
long long a[MX],b[MX];
int n,m,q;
int main(){
// #ifndef LOCAL
freopen("game.in","r",stdin);
freopen("game.out","w",stdout);
// #endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
static SGT F1(a,n);
for(int i=1;i<=m;i++){
cin>>b[i];
}
static SGT F2(b,m);
for(int i=1;i<=q;i++){
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
node ka=F1.ask(l1,r1);
node kb=F2.ask(l2,r2);
// cerr<<i<<' ';
if(ka.mn>0){
if(kb.mn>0){
cout<<ka.mx*kb.mn<<'\n';
}
else{
cout<<ka.mn*kb.mn<<'\n';
}
}
else if(ka.mx<0){
if(kb.mx>0){
cout<<ka.mx*kb.mx<<'\n';
}
else{
cout<<ka.mn*kb.mx<<'\n';
}
}
else{
if(kb.mn>0){
cout<<ka.mx*kb.mn<<'\n';
}
else if(kb.mx<0){
cout<<ka.mn*kb.mx<<'\n';
}
else{
if(ka.hz){
cout<<"0\n";
}
else{
// cerr<<ka.mnp<<' '<<ka.mxi<<' ';
cout<<max(ka.mnp*kb.mn,ka.mxi*kb.mx)<<"\n";
}
}
}
}
return 0;
}T3 galaxy
#include<bits/stdc++.h>
using namespace std;//+
const int MXN=5e5+10,MXM=5e5+10;
struct GRAPH{
int fst[MXN],nxt[MXM],v[MXM];
int tot;
void adde(int a,int b){
tot++;
nxt[tot]=fst[a];
fst[a]=tot;
v[tot]=b;
}
}G;
int n,m;
int od[MXN];
bool can[1010][1010];
void work1(){
int cnt=m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
G.adde(v,u);
can[u][v]=1;
od[u]++;
}
int q;
cin>>q;
while(q--){
int t,u,v;
cin>>t;
if(t==1){
cin>>u>>v;
can[u][v]=0;
od[u]--;
cnt--;
}
if(t==2){
cin>>u;
for(int i=G.fst[u];i;i=G.nxt[i]){
if(can[G.v[i]][u]){
can[G.v[i]][u]=0;
od[G.v[i]]--;
cnt--;
}
}
}
if(t==3){
cin>>u>>v;
can[u][v]=1;
od[u]++;
cnt++;
}
if(t==4){
cin>>u;
for(int i=G.fst[u];i;i=G.nxt[i]){
if(!can[G.v[i]][u]){
can[G.v[i]][u]=1;
od[G.v[i]]++;
cnt++;
}
}
}
bool f=(cnt==n);
if(f)
for(int i=1;i<=n;i++){
if(od[i]!=1){
f=0;
break;
}
}
if(f) cout<<"YES\n";
else cout<<"NO\n";
}
}
void work2(){
int cnt=m;
for(int i=1;i<=m;i++){
int u,v;
cin>>u>>v;
G.adde(u,v);
od[u]++;
}
int q;
cin>>q;
while(q--){
int t,u,v;
cin>>t;
if(t==1){
cin>>u>>v;
od[u]--;
cnt--;
}
else{
cin>>u>>v;
od[u]++;
cnt++;
}
bool f=(cnt==n);
if(f)
for(int i=1;i<=n;i++){
if(od[i]!=1){
f=0;
break;
}
}
if(f) cout<<"YES\n";
else cout<<"NO\n";
}
}
int main(){
// #ifndef LOCAL
freopen("galaxy.in","r",stdin);
freopen("galaxy.out","w",stdout);
// #endif
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n>>m;
if(n<=1000) work1();
else work2();
return 0;
}T4 transmit
#include<bits/stdc++.h>
using namespace std;//+
const int MXN=2e5+10,MXM=5e5+10;
struct GRAPH{
int fst[MXN],nxt[MXM],v[MXM];
int tot;
void adde(int a,int b){
tot++;
nxt[tot]=fst[a];
fst[a]=tot;
v[tot]=b;
}
}G;
int n,q,k;
int val[MXN];
int dfs(int s,int t,int f){
if(s==t){
return val[t];
}
int ans=0;
for(int i=G.fst[s];i;i=G.nxt[i]){
if(G.v[i]==f) continue;
ans+=dfs(G.v[i],t,s);
}
return ans?ans+val[s]:0;
}
int main(){
#ifndef LOCAL
freopen("transmit.in","r",stdin);
freopen("transmit.out","w",stdout);
#endif
cin>>n>>q>>k;
for(int i=1;i<=n;i++){
cin>>val[i];
}
for(int i=1;i<n;i++){
int a,b;
cin>>a>>b;
G.adde(a,b);
G.adde(b,a);
}
while(q--){
int s,t;
cin>>s>>t;
cout<<dfs(s,t,0)<<'\n';
}
return 0;
}