Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int prefer[maxn][maxn], wpartner[maxn], mfree[maxn];
- int N;
- bool wPreferM1OverM(int w, int m1, int m)
- {
- for (int i = 1; i <= N; i++)
- {
- if (prefer[w][i] == m1) return true;
- if (prefer[w][i] == m) return false;
- }
- }
- void stableMarriage()
- {
- memo(wpartner, -1);
- memo(mfree, 0);
- int freeCount = N;
- while (freeCount)
- {
- int m;
- for (m = 1; m <= N; m++)
- {
- if (mfree[m] == 0)
- break;
- }
- for (int i = 1; i <= N && mfree[m] == 0; i++)
- {
- int w = prefer[m][i];
- if (wpartner[w] == -1)
- {
- wpartner[w] = m;
- mfree[m] = 1;
- freeCount--;
- }
- else
- {
- int m1 = wpartner[w];
- if (wPreferM1OverM(w, m1, m) == false)
- {
- wpartner[w] = m;
- mfree[m] = 1;
- mfree[m1] = 0;
- }
- }
- }
- }
- int woman[maxn];
- for (int i = N; i <= 2*N; i++)
- woman[ wpartner[i] ] = i-N;
- for (int i = 1; i <= N; i++)
- printf("%d %d\n", i, woman[i]);
- }
- int main()
- {
- int tc;
- scanf("%d", &tc);
- for (int tcase = 1; tcase <= tc; tcase++)
- {
- scanf("%d", &N);
- for (int i = 1; i <= N; i++) // woman preferences
- {
- int w;
- scanf("%d", &w);
- w += N;
- for (int j = 1; j <= N; j++)
- scanf("%d", &prefer[w][j]);
- }
- for (int i = 1; i <= N; i++) // man preferences
- {
- int m;
- scanf("%d", &m);
- for (int j = 1; j <= N; j++)
- {
- int w;
- scanf("%d", w);
- prefer[m][j] = w+N;
- }
- }
- stableMarriage();
- }
- } int prefer[maxn][maxn], wpartner[maxn], mfree[maxn];
- int N;
- bool wPreferM1OverM(int w, int m1, int m)
- {
- for (int i = 1; i <= N; i++)
- {
- if (prefer[w][i] == m1) return true;
- if (prefer[w][i] == m) return false;
- }
- }
- void stableMarriage()
- {
- memo(wpartner, -1);
- memo(mfree, 0);
- int freeCount = N;
- while (freeCount)
- {
- int m;
- for (m = 1; m <= N; m++)
- {
- if (mfree[m] == 0)
- break;
- }
- for (int i = 1; i <= N && mfree[m] == 0; i++)
- {
- int w = prefer[m][i];
- if (wpartner[w] == -1)
- {
- wpartner[w] = m;
- mfree[m] = 1;
- freeCount--;
- }
- else
- {
- int m1 = wpartner[w];
- if (wPreferM1OverM(w, m1, m) == false)
- {
- wpartner[w] = m;
- mfree[m] = 1;
- mfree[m1] = 0;
- }
- }
- }
- }
- int woman[maxn];
- for (int i = N; i <= 2*N; i++)
- woman[ wpartner[i] ] = i-N;
- for (int i = 1; i <= N; i++)
- printf("%d %d\n", i, woman[i]);
- }
- int main()
- {
- int tc;
- scanf("%d", &tc);
- for (int tcase = 1; tcase <= tc; tcase++)
- {
- scanf("%d", &N);
- for (int i = 1; i <= N; i++) // woman preferences
- {
- int w;
- scanf("%d", &w);
- w += N;
- for (int j = 1; j <= N; j++)
- scanf("%d", &prefer[w][j]);
- }
- for (int i = 1; i <= N; i++) // man preferences
- {
- int m;
- scanf("%d", &m);
- for (int j = 1; j <= N; j++)
- {
- int w;
- scanf("%d", w);
- prefer[m][j] = w+N;
- }
- }
- stableMarriage();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement