#P14500. [NCPC 2025] Follower Forensics

[NCPC 2025] Follower Forensics

题目描述

Your social media company ConnectHub prides itself on how easily information spreads through its network.

:::align{center} :::

Accounts can share posts directly with their followers using the share button, or with the accounts they follow by posting in the comments. ConnectHub is famous for the virality of its users: a post from any account was able to reach every other account, possibly through a chain of shares and comments.

But disaster struck: a database mishap wiped out all records of who follows whom. One piece of data may have survived, though — for each account, you have the number of its followers and the number of accounts it follows. Maybe even those numbers have been corrupted, but it's all you have.

Your task is to rebuild the missing database so that these counts are correct, and ConnectHub once again has full virality.

输入格式

The input consists of:

  • One line with an integer nn, the number of accounts, where 1n1000001 \leq n \leq 100\,000.
  • One line with nn integers a1,,ana_1,\ldots, a_n satisfying 0ain0 \leq a_i \leq n for each ii, where aia_i is the number of accounts followed by the iith account.
  • One line with nn integers b1,,bnb_1,\ldots, b_n satisfying 0bin0 \leq b_i \leq n for each ii, where bib_i is the number of accounts following the iith account.

Additionally, it is guaranteed that

$$\sum_{i=1}^n a_i + \sum_{i=1}^n b_i \leq 2\,000\,000.$$

Note that the input is not guaranteed to allow for any reconstruction, fully viral or not. For those familiar with that terminology, full virality is equivalent to weak connectivity.

输出格式

If a reconstruction is possible, output a network

  • One line with two integers: the number nn of accounts, and the number mm of connections. This is the same nn as in the input.
  • Then mm lines, each consisting of two integers aa and bb, specifying that account aa follows account bb.

Otherwise output impossible\texttt{impossible}.

Note that it is not allowed for an account to follow the same account twice, nor to follow itself.

5
2 1 2 1 3
0 2 2 3 2
5 9
2 4
4 5
3 5
3 2
5 2
1 3
1 4
5 3
5 4
3
2 2 1
1 1 2

impossible