数据过水
查看原帖
数据过水
503792
Svemit楼主2023/7/1 10:50

我的code样例输出无解,但是交上去过了,建议把样例加进测试点,顺便求调

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 5, INF = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
int n = 52, m;
vector<pair<int, int> > e[N];
int id(char c)
{
	if(c >= 'A' && c <= 'Z') return c - 'A' + 1;
	else return c - 'a' + 27;
}
bool st[N];
int deg[N], h[N];
stack<int> s;
void dfs(int u)
{
	for(int &i = h[u]; i < e[u].size();) 
	{
		auto t = e[u][i ++];
		if(!st[t.second]) 
			st[t.second] = true, dfs(t.first);
	}
	s.push(u);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> m;
    for(int i = 1; i <= m; i ++)
    {
    	string s;
    	cin >> s;
    	int u = id(s[0]), v = id(s[1]);
    	e[u].push_back({v, i}), e[v].push_back({u, i});
    	deg[u] ++, deg[v] ++;
    }
  	for(int i = 1; i <= n; i ++) 
  		sort(e[i].begin(), e[i].end());
  	int cnt = 0, cur = 1;
  	for(int i = n; i >= 1; i --)
  		if(deg[i] & 1) cnt ++, cur = i;
  	if(cnt == 2 || cnt == 0)
  	{
  		dfs(cur);
  		if(s.size() < m + 1) cout << "No Solution\n";
  		else while(s.size())
  		{
  			if(s.top() > 26) cout << char('a' + s.top() - 27);
  			else cout << char('A' + s.top() - 1);
  			s.pop();
  		}
  	} 
  	else cout << "No Solution\n";
    return 0;
}
2023/7/1 10:50
加载中...