102 条题解
-
-1
#include<bits/stdc++.h> using namespace std; const int N = 510; int n, m; int d[N]; bool g[N][N]; int stop[N]; int q[N]; void bfs(){ memset(d, 0x3f, sizeof d); //初始化距离 q[0] = 1; d[1] = 0; int hh = 0, tt = 0; while(hh <= tt){ int t = q[hh ++ ]; for(int i = 1; i <= n; i ++ ){ //之前我一直在迷惑为什么1~n的换乘次数会等于1~n的乘车次数-1 //其实道理很简单,即从x~y点的最近距离要么是1,要么是换乘次数最少的距离 //如果x、y在同一条线路,那最短距离肯定是1 //如果不在同一条线路,那最短距离就会被换乘的站点之间更新,因为一直在找最小距离 //所以1~n的换乘次数会等于1~n的乘车次数-1 if(d[i] > d[t] + 1 && g[t][i]){ //如果点i和点t之间有通路且通过t可以找到到达i的更小距离 d[i] = d[t] + 1; q[ ++ tt] = i; } } } } int main() { cin>>m>>n; string line; getline(cin, line); //读取m、n后面的空格/换行符 while(m -- ){ getline(cin, line); //读入一行 stringstream ssin(line); //定义字符串流ssin,用line初始化ssin(相当于赋值) int cnt = 0, p; while(ssin>>p) stop[cnt ++ ] = p; //将同一条线路的每个站点和之后的每个站点连一条线 for(int i = 0; i < cnt; i ++ ){ for(int j = i + 1; j < cnt; j ++ ){ g[stop[i]][stop[j]] = true; } } } bfs(); if(d[n] == 0x3f3f3f3f) cout<<"NO"<<endl; else cout<<max(d[n] - 1, 0)<<endl; return 0; }
-
-1
#include<bits/stdc++.h> using namespace std; int read(){ int ans=0,f=1; char ch=getchar(); while(!isdigit(ch)) f *= (ch=='-') ? -1 : 1, ch = getchar(); while(isdigit(ch)) ans = ((ans<<1)+(ans<<3)+(ch^48)),ch = getchar(); return ans*f; } const int MAXX = 1e3; int N, M, sum, cnt; int g[MAXX][MAXX], flag[MAXX]; std:: queue <int> q; void dfs(int x){ if(x==1) printf("%d",x); else printf("-%d",x); sum++; flag[x] = 1; for(int i=1;i<=N;i++){ // if(!g[x][i]) continue; // if(flag[i]) continue; if(g[x][i] && (!flag[i])) dfs(i); } } int main(){ N = read(), M = read(); for(int i=1;i<=M;i++){ // int x = read(), y = read(); int x,y; cin>>x>>y; g[x][y] = 1; } dfs(1); while(sum+1<=N){ dfs(sum); }; printf("\n"); memset(flag,0,sizeof(flag)); q.push(1); flag[1] = 1; while(!q.empty()){ int F = q.front(); for(int i=1; i<=N; i++){ if(g[F][i]==1 && (flag[i]==0)){ q.push(i); flag[i] = 1; } } if(F==1) printf("%d",F); else printf("-%d",F); q.pop(); cnt++; } while(cnt<=N){ while(!q.empty()){ int F = q.front(); for(int i=1; i<=N; i++){ if(g[F][i]==1 && (flag[i]==0)){ q.push(i); flag[i] = 1; } } if(F==1) printf("%d",F); else printf("-%d",F); q.pop(); cnt++; } } // for(int i=1;i<=N;i++){ // for(int j=1;j<=N;j++){ // printf("%d ",g[i][j]); // } // printf("\n"); // } return 0; }
-
-1
-
-1
-
-2
终于可以写一份A+B
这么难的题的题解了。咦?竟然没有人写LCT的题解?
Link-Cut Tree 会很伤心的!
ORZ为了不让LCT伤心于是我来一份LCT的A+B题解吧!
送上代码:`
#include <cstring> #include <cstdio> #include <cstring> using namespace std; struct node { int data,rev,sum; node *son[2],*pre; bool judge(); bool isroot(); void pushdown(); void update(); void setson(node *child,int lr); }lct[233]; int top,a,b; node *getnew(int x) { node *now=lct+ ++top; now->data=x; now->pre=now->son[1]=now->son[0]=lct; now->sum=0; now->rev=0; return now; } bool node::judge(){return pre->son[1]==this;} bool node::isroot() { if(pre==lct)return true; return !(pre->son[1]==this||pre->son[0]==this); } void node::pushdown() { if(this==lct||!rev)return; swap(son[0],son[1]); son[0]->rev^=1; son[1]->rev^=1; rev=0; } void node::update(){sum=son[1]->sum+son[0]->sum+data;} void node::setson(node *child,int lr) { this->pushdown(); child->pre=this; son[lr]=child; this->update(); } void rotate(node *now) { node *father=now->pre,*grandfa=father->pre; if(!father->isroot()) grandfa->pushdown(); father->pushdown();now->pushdown(); int lr=now->judge(); father->setson(now->son[lr^1],lr); if(father->isroot()) now->pre=grandfa; else grandfa->setson(now,father->judge()); now->setson(father,lr^1); father->update();now->update(); if(grandfa!=lct) grandfa->update(); } void splay(node *now) { if(now->isroot())return; for(;!now->isroot();rotate(now)) if(!now->pre->isroot()) now->judge()==now->pre->judge()?rotate(now->pre):rotate(now); } node *access(node *now) { node *last=lct; for(;now!=lct;last=now,now=now->pre) { splay(now); now->setson(last,1); } return last; } void changeroot(node *now) { access(now)->rev^=1; splay(now); } void connect(node *x,node *y) { changeroot(x); x->pre=y; access(x); } void cut(node *x,node *y) { changeroot(x); access(y); splay(x); x->pushdown(); x->son[1]=y->pre=lct; x->update(); } int query(node *x,node *y) { changeroot(x); node *now=access(y); return now->sum; } int main() { scanf("%d%d",&a,&b); node *A=getnew(a); node *B=getnew(b); //连边 Link connect(A,B); //断边 Cut cut(A,B); //再连边orz Link again connect(A,B); printf("%d\n",query(A,B)); return 0; }
-
-2
[ ] 54 道题
- 1
状态 题目显示标签 AC / 尝试 难度[](javascript:;) -------------------------------------------------------- - 1
[ 进入编辑模式](javascript:;)
分类
根据当前过滤条件随机选择一道题
状态
开发
支持
-
Language
-
主题
-
Worker 0 in 23ms
-
Powered by Hydro v4.7.2 Community
信息
- ID
- 1
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 843
- 已通过
- 300
- 上传者