4839 -- Traffic Real Time Query System
2017-02-10 19:28
567 查看
真的没必要再在上面花时间了,交标程的事我也不想干,就把代码贴这里算了。
#pragma GCC optimize("O2") #include<cstdio> #include<iostream> #include<algorithm> #include<ctime> #include<cstdlib> #include<cmath> using namespace std; #define rep(i,j,k) for(i=j;i<=k;++i) #define per(i,j,k) for(i=j;i>=k;--i) #define ll long long #define db double #define ldb long double #define pii pair<int,int> #define mkp make_pair #define X first #define Y second const int N=10005,M=100005; int n,m; struct POINT{int K,ne;}P[M<<1],Pt[M<<1]; int he ,tot,het[N<<1],tott; inline void add(int x,int y){ P[++tot]=(POINT){y,he[x]};he[x]=tot; P[++tot]=(POINT){x,he[y]};he[y]=tot; } inline void addt(int x,int y){ Pt[++tott]=(POINT){y,het[x]};het[x]=tott; Pt[++tott]=(POINT){x,het[y]};het[y]=tott; } int dfn ,low ,idx,blg[M],blk,tree ,rt ,sec,ic ,stk[M],Tp,vis[M<<1]; void tarjan(int x){ int y,z,p,so=0; dfn[x]=low[x]=++idx; for(p=he[x];p;p=P[p].ne)if(!vis[p]){ stk[++Tp]=p,vis[p]=vis[p^1]=1; if(!dfn[y=P[p].K]){ tarjan(y); if(low[y]>=dfn[x]){ ++so;tree[++blk]=sec; do{blg[(z=stk[Tp--])>>1]=blk;}while(z!=p); } low[x]=min(low[x],low[y]); } else low[x]=min(dfn[y],low[x]); } if(rt[sec]==x?so>1:so) ic[x]=++blk; } int pre[N<<1],son[N<<1],sz[N<<1],dad[N<<1],dep[N<<1]; void DFS1(int x,int fa){ int y,p;sz[x]=1;dep[x]=dep[fa]+1;pre[x]=fa; for(p=het[x];p;p=Pt[p].ne)if(Pt[p].K!=fa){ DFS1(y=Pt[p].K,x);sz[x]+=sz[y]; if(sz[y]>sz[son[x]])son[x]=y; } } void DFS2(int x,int fa){ int y,p; dad[x]=son[fa]==x?dad[fa]:x; for(p=het[x];p;p=Pt[p].ne)if(Pt[p].K!=fa) DFS2(y=Pt[p].K,x); } inline int lca(int x,int y){ int fx=dad[x],fy=dad[y]; while(fx!=fy) if(dep[fx]>dep[fy]) fx=dad[x=pre[fx]]; else fy=dad[y=pre[fy]]; return dep[x]>dep[y]?y:x; } int main(){ // freopen("r.in","r",stdin); // freopen("w.out","w",stdout); int i,x,y,z,p,Q;int cnt=0; do{ rep(i,1,n)he[i]=ic[i]=dfn[i]=0; rep(i,1,blk)het[i]=son[i]=0; rep(i,2,tot)vis[i]=0; idx=blk=sec=0; tott=tot=1; scanf("%d%d",&n,&m); if(!n&&!m)break; if((++cnt)>10)break; rep(i,1,m){ scanf("%d%d",&x,&y); add(x,y); } rep(i,1,n)if(!dfn[i])tarjan(rt[++sec]=i); rep(i,1,n) if(x=ic[i]) for(p=he[i];p;p=P[p].ne) addt(x,blg[p>>1]); rep(i,1,sec)if(he[rt[i]]){ x=blg[he[rt[i]]>>1]; DFS1(x,0);DFS2(x,0); } for(scanf("%d",&Q);Q--;){ scanf("%d%d",&x,&y); z=lca(x=blg[x],y=blg[y]); printf("%d\n",dep[x]+dep[y]-(dep[z]<<1)>>1); } }while(1); return 0; }
相关文章推荐
- UVALive 4839 HDU 3686 Traffic Real Time Query System
- UVALive 4839 HDU 3686 Traffic Real Time Query System
- HDU Traffic Real Time Query System
- HDU3686 Traffic Real Time Query System
- UVALive-4839 HDU-3686 Traffic Real Time Query System 题解
- HDU 3686 Traffic Real Time Query System(双连通分量缩点+LCA)(2010 Asia Hangzhou Regional Contest)
- HDU3686 Traffic Real Time Query System
- Hdu 3686 Traffic Real Time Query System(双联通分量+LCA)
- HDU 3686 Traffic Real Time Query System(点双连通缩点 + LCA)
- uva 1464 - Traffic Real Time Query System(双联通+LCA)
- 【HDOJ】3686 Traffic Real Time Query System
- 【HDU】3686 Traffic Real Time Query System 点双连通+LCA
- HDU Traffic Real Time Query System
- Inotify: Efficient, Real-Time Linux File System Event Monitoring
- System.currentTimeMillis() uptimeMillis elapsedRealtime 区别
- windows平台时间函数性能比较QueryPerformanceCounter,GetTickCount,ftime,time,GetLocalTime,GetSystemTimeAsFileTim
- System.currentTimeMillis() uptimeMillis elapsedRealtime 区别
- 一步一步跟我学习lucene(19)---lucene增量更新和NRT(near-real-time)Query近实时查询
- 1)实际时间(real time): 从command命令行开始执行到运行终止的消逝时间; 2)用户CPU时间(user CPU time): 命令执行完成花费的用户CPU时间,即命令在用户态中执行时间总和; 3)系统CPU时间(system CPU time): 命令执行完成花费的系统CPU时
- SystemClock.elapsedRealtime()