P1347 排序
1.F. Chat Screenshots2.P1656 炸铁路3.P1137 旅行计划4.P2835 刻录光盘5.P1197 [JSOI2008] 星球大战6.P3388 【模板】割点(割顶)7.P8435 【模板】点双连通分量8.P8436 【模板】边双连通分量9.P2860 [USACO06JAN] Redundant Paths G10.P1653 [USACO04DEC] Cow Ski Area G11.P3047 [USACO12FEB] Nearby Cows G12.P1894 [USACO4.2] 完美的牛栏The Perfect Stall13.P1550 [USACO08OCT] Watering Hole G14.P2330 [SCOI2005] 繁忙的都市15.P1525 [NOIP2010 提高组] 关押罪犯16.P1379 八数码难题17.P2746 [USACO5.3] 校园网Network of Schools18.P6121 [USACO16OPEN] Closing the Farm G19.P2341 [USACO03FALL / HAOI2006] 受欢迎的牛 G20.P2055 [ZJOI2009] 假期的宿舍21.P5905 【模板】全源最短路(Johnson)22.F. Microcycle23.G. Path Prefixes24.G. Rudolf and Subway25.C. Ehab and Path-etic MEXs26.A. String Transformation 127.D. Secret Passwords28.F. Maximum White Subtree29.P3478 [POI2008] STA-Station
30.P1347 排序
31.P1960 郁闷的记者32.E1. Weights Division (easy version)33.P5007 DDOSvoid 的疑惑34.P2850 [USACO06DEC] Wormholes G35.P1265 公路修建36.P1354 房间最短路问题37.P2168 [NOI2015] 荷马史诗38.P8306 【模板】字典树39.P1481 魔族密码40.P3128 [USACO15DEC] Max Flow P41.P5536 【XR-3】核心城市42.P5836 [USACO19DEC] Milk Visits S43.P3384 【模板】重链剖分/树链剖分44.P5960 【模板】差分约束45.P7771 【模板】欧拉路径46.六度分离47.整数区间48.F. Alex's whims49.J. 上学50.Game on Tree51.E. We Need More Bosses52.B. Omkar and Heavenly Tree53.B. Mahmoud and Ehab and the bipartiteness54.P1668 [USACO04DEC] Cleaning Shifts S55.P6154 游走56.P8655 [蓝桥杯 2017 国 B] 发现环57.P10298 [CCC 2024 S4] Painting Roads58.P9650 [SNCPC2019] Escape Plan59.P9327 [CCC 2023 S4] Minimum Cost Roads60.P9026 [CCC2021 S4] Daily Commute61.P8724 [蓝桥杯 2020 省 AB3] 限高杆62.P4878 [USACO05DEC] Layout G63.P5663 [CSP-J2019] 加工零件64.P2731 [USACO3.3] 骑马修栅栏 Riding the Fences65.I. Disks66.P1351 [NOIP2014 提高组] 联合权值67.B. Time Travel68.F. Minimum Maximum Distance69.A. Book70.P1407 [国家集训队] 稳定婚姻71.P1991 无线通讯网72.P4047 [JSOI2010] 部落划分73.P3275 [SCOI2011] 糖果74.P1989 无向图三元环计数75.P1967 [NOIP2013 提高组] 货车运输76.D. Vitaly and Cycle77.P10838 『FLA - I』庭中有奇树78.P9751 [CSP-J 2023] 旅游巴士79.D. Colored Portals题解
1.
2.由于数据范围小,所以可以输入一次进行一次拓扑遍历
3.如果存在矛盾,说明存在环
4.对于拓扑排序进行层次标记,如果最大层等于n,代表每个字母层次分明,有先后次序
code
#include<bits/stdc++.h>
using namespace std;
vector<int> G[30];
int in_copy[30]={0};
int in[30]={0};
struct node
{
int id,layer;
};
int n,m;
int tuopu()
{
queue<node> q;
for(int i=0;i<n;i++) if(!in_copy[i]&&G[i].size()) q.push({i,1});
int maxlayer=0;
while(q.size())
{
int now=q.front().id,layer=q.front().layer;
q.pop();
maxlayer=max(maxlayer,layer);
for(auto next:G[now])
{
in_copy[next]--;
if(!in_copy[next]) q.push({next,layer+1});
}
}
for(int i=0;i<26;i++) if(in_copy[i])return -1;
//printf("%d %d\n",maxlayer,n);
return (maxlayer==n);
}
void print()
{
queue<int> q;
for(int i=0;i<26;i++) if(!in[i]&&G[i].size()) q.push(i);
while(q.size())
{
int now=q.front();
q.pop();
printf("%c",now+65);
for(auto next:G[now])
{
in[next]--;
if(!in[next]) q.push(next);
}
}
}
int main()
{
cin>>n>>m;
int ends=0;
for(int i=1;i<=m;i++)
{
char a,op,b;
cin>>a>>op>>b;
if(ends) continue;
int a1=a-'A',b1=b-'A';
G[a1].emplace_back(b1);
in[b1]++;
for(int i=0;i<26;i++) in_copy[i]=in[i];
int tag=tuopu();
if(tag==-1)
{
printf("Inconsistency found after %d relations.\n",i);
ends=1;
}
else if(tag==1)
{
printf("Sorted sequence determined after %d relations: ",i);
print();
puts(".");
ends=1;
}
}
if(!ends) printf("Sorted sequence cannot be determined.");
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本