Description:
有一个环形道路,城市
i
i
与城市相连,每个城市之间有
disi
d
i
s
i
,每个城市有补给
vali
v
a
l
i
.现在询问对于每个城市
i
i
做为起点,能否完成环游一圈,即再回到起点.
n≤106
n
≤
10
6
Solution:
Code:
#include<bits/stdc++.h>
using namespace std;
#define REP(i,f,t)for(int i=(f),i##_end_=(t);i<=i##_end_;++i)
#define SREP(i,f,t)for(int i=(f),i##_end_=(t);i<i##_end_;++i)
#define DREP(i,f,t)for(int i=(f),i##_end_=(t);i>=i##_end_;--i)
#define db double
#define ll long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define MINF 0xc0c0c0c0
#define Sz(A) sizeof(A)
#define mcl(A,b) memset(A,b,Sz(A))
#define mcp(A,b) memcpy(A,b,Sz(b))
#define pb push_back
#define fi first
#define se second
template<class T>inline bool chkmin(T &x,T y){return y<x?x=y,1:0;}
template<class T>inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
typedef pair<int,int>PII;
template<class T>inline void Rd(T &x){
x=0;char c;
while((c=getchar())<48);
do x=(x<<1)+(x<<3)+(c^48);
while((c=getchar())>47);
}
#define N 2000002
int n;
int val[N];
struct p100{
int dis[N];
ll sum[N];
int Q[N],L,R;
bool f[N],g[N];
void go(bool *ans){
REP(i,1,n) val[i+n]=val[i],dis[i+n]=dis[i];
REP(i,1,n<<1) sum[i]=sum[i-1]+val[i]-dis[i];
L=R=0;
REP(i,1,n){
while(R && sum[i]<sum[Q[R-1]]) --R;
Q[R++]=i;
}
REP(i,1,n) {
if(Q[L]<i) ++L;
ans[i]=(sum[Q[L]]-sum[i-1]>=0);
while(L<R && sum[i+n]<sum[Q[R-1]]) --R;
Q[R++]=i+n;
}
}
void solve(){
REP(i,1,n)Rd(val[i]),Rd(dis[i]);
go(f);
reverse(val+1,val+1+n);
reverse(dis+1,dis+1+n);
SREP(i,0,n)dis[i]=dis[i+1];
dis[n]=dis[0];
go(g);
ll ans=0;
REP(i,1,n)ans+=(f[i] || g[n-i+1])?i:0;
printf("%lld\n",ans);
}
}p2;
int main(){
// freopen("satellite.in","r",stdin);
// freopen("satellite.out","w",stdout);
scanf("%d",&n);
p2.solve();
return 0;
}
Description:
有
n
n
个人,其中有一个人是杀手,其余都是平民.现在有个关系,表示
x
x
知道的身份.
现在警察要在策略最优的情况下查出杀手并保证自己的安全.注意:警察一查到该人是杀手,那警察就会被杀!求查出杀手的概率.
n≤200000,m≤300000
n
≤
200000
,
m
≤
300000
Solution:
Code:
#include<bits/stdc++.h>
using namespace std;
#define REP(i,f,t)for(int i=(f),i##_end_=(t);i<=i##_end_;++i)
#define SREP(i,f,t)for(int i=(f),i##_end_=(t);i<i##_end_;++i)
#define DREP(i,f,t)for(int i=(f),i##_end_=(t);i>=i##_end_;--i)
#define db double
#define ll long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define MINF 0xc0c0c0c0
#define Sz(A) sizeof(A)
#define mcl(A,b) memset(A,b,Sz(A))
#define mcp(A,b) memcpy(A,b,Sz(b))
#define pb push_back
#define fi first
#define se second
template<class T>inline bool chkmin(T &x,T y){return y<x?x=y,1:0;}
template<class T>inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
typedef pair<int,int>PII;
template<class T>inline void Rd(T &x){
x=0;char c;
while((c=getchar())<48);
do x=(x<<1)+(x<<3)+(c^48);
while((c=getchar())>47);
}
#define N 200002
#define M 300002
int n,m;
int qwq,head[N];
struct edge{
int to,nxt;
}E[M<<1];
inline void addedge(int x,int y){E[qwq]=(edge){y,head[x]};head[x]=qwq++;}
vector<int>G[N];
int dfn[N],low[N],tim;
int stk[N],top;
int Id[N],tot;
bool vis[N];
int sum[N],degree[N];
map<int,bool>mp[N];
void tarjan(int x){
dfn[x]=low[x]=++tim;
stk[++top]=x;
vis[x]=1;
for(int i=head[x];~i;i=E[i].nxt){
int y=E[i].to;
if(!dfn[y]){
tarjan(y);
chkmin(low[x],low[y]);
}
else if(vis[y])chkmin(low[x],dfn[y]);
}
if(dfn[x]==low[x]){
tot++;
do{
Id[stk[top]]=tot;
sum[tot]++;
vis[stk[top]]=0;
}while(x!=stk[top--]);
}
}
int main(){
// freopen("reisen.in","r",stdin);
// freopen("reisen.out","w",stdout);
mcl(head,-1);
scanf("%d%d",&n,&m);
REP(i,1,m){
int x,y;
Rd(x),Rd(y);
addedge(x,y);
}
REP(i,1,n) if(!dfn[i]) tarjan(i);
REP(x,1,n){
for(int i=head[x];~i;i=E[i].nxt){
int y=E[i].to;
if(Id[x]!=Id[y] && !mp[Id[x]][Id[y]]){
mp[Id[x]][Id[y]]=1;
G[Id[x]].pb(Id[y]);
degree[Id[y]]++;
}
}
}
int res=0;
bool find=0;
REP(i,1,tot) res+=(!degree[i]);
REP(i,1,tot){
if(sum[i]==1 && !degree[i]){
bool chk=0;
SREP(j,0,G[i].size()){
int y=G[i][j];
if(degree[y]>1)chk=1;
else {chk=0;break;}
}
if(chk)find=1;
}
if(find){
res--;
break;
}
}
if(!m)printf("%.6lf\n",(db)1/n);
else if(n==1)puts("1.000000");
else printf("%.6lf\n",(db)(n-res)/n);
return 0;
}
Description:
有一个
n
n
个点,条边的的图,特殊地,每条边有颜色.
而
x
x
到的花费为上次边的颜色与当前边的颜色不同时为
1
1
,否则为.
求
1
1
到的最小花费.
n,m≤500000
n
,
m
≤
500000
Solution:
Code:
#include<bits/stdc++.h>
using namespace std;
#define REP(i,f,t)for(int i=(f),i##_end_=(t);i<=i##_end_;++i)
#define SREP(i,f,t)for(int i=(f),i##_end_=(t);i<i##_end_;++i)
#define DREP(i,f,t)for(int i=(f),i##_end_=(t);i>=i##_end_;--i)
#define db double
#define ll long long
#define INF 0x3f3f3f3f
#define inf 0x3f3f3f3f3f3f3f
#define MINF 0xc0c0c0c0
#define Sz(A) sizeof(A)
#define mcl(A,b) memset(A,b,Sz(A))
#define mcp(A,b) memcpy(A,b,Sz(b))
#define pb push_back
#define fi first
#define se second
template<class T>inline bool chkmin(T &x,T y){return y<x?x=y,1:0;}
template<class T>inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
typedef pair<int,int>PII;
template<class T>inline void Rd(T &x){
x=0;char c;
while((c=getchar())<48);
do x=(x<<1)+(x<<3)+(c^48);
while((c=getchar())>47);
}
#define N 500002
#define M 1000002
int n,m;
int qwq,head[N];
struct edge{
int to,nxt;
}E[N<<2];
void addedge(int x,int y){E[qwq]=(edge){y,head[x]};head[x]=qwq++;}
vector<int>X[M],Y[M],G[2][N];
int tot;
int vis[N];
int stk[N],top;
int dis[N];
queue<int>Q[2];
void dfs(int x){
int y;
vis[x]=1;
G[1][x].pb(tot);
G[0][tot].pb(x);
for(int i=head[x];~i;i=E[i].nxt){
int y=E[i].to;
if(vis[y])continue;
dfs(y);
}
}
void Init(int col){
int x,y;
top=qwq=0;
mcl(head,-1);
SREP(i,0,X[col].size()){
x=X[col][i],y=Y[col][i];
addedge(x,y);
addedge(y,x);
stk[++top]=x,stk[++top]=y;
}
REP(i,1,top)if(!vis[stk[i]]){
++tot;
dfs(stk[i]);
}
REP(i,1,top){
vis[stk[i]]=0;
head[stk[i]]=-1;
}
}
int bfs(){
int cur=0;
mcl(dis,INF);
Q[cur].push(1);
dis[1]=0;
while(!Q[cur].empty()||!Q[!cur].empty()){
if(Q[cur].empty())cur=!cur;
int x=Q[cur].front(),y;Q[cur].pop();
SREP(i,0,G[0][x].size()){
int y=G[0][x][i];
if(dis[y]==INF){
dis[y]=dis[x];
Q[cur].push(y);
}
}
SREP(i,0,G[1][x].size()){
int y=G[1][x][i];
if(dis[y]==INF){
dis[y]=dis[x]+1;
Q[!cur].push(y);
}
}
}
return dis[n]==INF?-1:dis[n];
}
int main(){
// freopen("subway.in","r",stdin);
// freopen("subway.out","w",stdout);
Rd(n),Rd(m);
tot=n;
int mx=0,a,b,c;
REP(i,1,m){
Rd(a),Rd(b),Rd(c);
chkmax(mx,c);
X[c].pb(a);
Y[c].pb(b);
}
REP(i,1,mx) Init(i);
printf("%d\n",bfs());
return 0;
}
Summary: