衡水wap网站建设费用网站优化包括哪些
调了好长时间,给我调哈气了。
要么是内存超限、数组越界,就是WA,样例都过了。
原题:西邮第五届ACM校赛-B烦人的依赖
参考
#include <bits/stdc++.h>
#define forr(i,l,r) for(int i=l;i<=r;i++)
#define reforr(i,l,r) for(int i=r;i>=l;i--)
#define int long long
#define pii pair<int,int>
#define endl '\n'
#define PI 3.14159265
using namespace std;
const int N=3e4+10,M=1e5+10;//3e4开二维数组太大了 链式前向星存储
int head[N],deg[N];
char c[N];
int cnt,ca=1;;
struct Edge{int to,nxt;
}edge[M];void addedge(int u,int v){edge[cnt].to=v;edge[cnt].nxt=head[u];//链接u的上一个邻居head[u]=cnt++;//记录本次邻居位置
}
void solve(){cnt=0;memset(head,-1,sizeof head);memset(deg,0,sizeof deg);unordered_map<char,int>rec;int n,m;cin>>n>>m;// vector<int>deg(n+10,0);// vector<char>c(n+10);forr(i,1,n){cin>>c[i];rec[c[i]]=i;//给字母编号}char u,v;forr(i,1,m){cin>>u>>v;int nu=rec[u],nv=rec[v];addedge(nu,nv);deg[nv]++;}//bfspriority_queue<char,vector<char>,greater<char>>q;forr(i,1,n)if(!deg[i])q.push(c[i]);vector<char>ans;while(!q.empty()){char now=q.top();q.pop();ans.push_back(now);//加入答案//找邻居int ni=rec[now];for(int i=head[ni];i!=-1;i=edge[i].nxt){int nb=edge[i].to;deg[nb]--;if(!deg[nb])q.push(c[nb]);//如果入度成了0}}cout<<"Case #"<<ca++<<":"<<endl;if(ans.size()!=n)cout<<"Impossible"<<endl;else{for(auto i:ans)cout<<i<<endl;}
}
signed main()
{
// ios::sync_with_stdio(false), std::cin.tie(nullptr), std::cout.tie(nullptr);int _ ;_ = 1;cin>>_;while (_--){solve();}return 0;
}