WA 19 个点求助
查看原帖
WA 19 个点求助
1094441
封禁用户楼主2024/3/16 17:08
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <algorithm>
using namespace std;

const int MAXN = 100005;
vector<int> graph[MAXN];
int match[MAXN];
bool vis[MAXN];
string comments[MAXN];
map<char, int> charIndex;

bool dfs(int u) 
{
    for (int i = 0; i < graph[u].size(); ++ i) 
	{
        int v = graph[u][i];
        if (v > 0 && !vis[v]) 
		{
            vis[v] = true;
            if (match[v] == -1 || dfs(match[v])) 
			{
                match[v] = u;
                return true;
            }
        }
    }
    return false;
}

int hungarian(int n) 
{
    int cnt = 0;
    fill(match, match + MAXN, -1);
    for (int i = 1; i <= n; ++ i)
	{
        fill(vis, vis + MAXN, false);
        if (dfs(i)) 
		{
            ++ cnt;
        }
    }
    return cnt;
}

int main() 
{
    int n;
    cin >> n;

    for (int i = 0; i < n; i++) 
	{
        cin >> comments[i];
    }

    for (int i = 0; i < n; i++) 
	{
        for (int j = 0; j < n; j++) 
		{
            charIndex[comments[i][j]] += j - i;
        }
    }

    for (char c = 'a'; c <= 'z'; ++ c) 
	{
        for (int offset = 0; offset < n; ++ offset) 
		{
            for (int i = 1; i <= n; ++ i) 
			{
                graph[i].clear();
            }

            for (int i = 0; i < n; ++ i) 
			{
                for (int j = 0; j < n; ++ j) 
				{
                    if (comments[i][j] == c) 
					{
                        graph[i + 1].push_back(j - i + offset + 1);
                    }
                }
            }

            if (hungarian(n) == n) 
			{
                cout << "YES" << endl;
                return 0;
            }
        }
    }

    cout << "NO" << endl;

    return 0;
}

2024/3/16 17:08
加载中...