HEOI2013 bzoj3168 Calcium Iron Zinc Selenium Vitamin __ matrix

Source: Internet
Author: User
Tags pow first row

Description

The list of the Galaxy contestants has come out. Kobayashi, as a distinguished dietitian, will be in charge of the Galaxy players
Cosmic Game of diet. It is well known that a planet that travels to the universe usually takes a long time to change, so it is necessary to match the food to the daily conditions to ensure nutrition. Kobayashi divides the nutritional needs of the human body into N species, including but not limited to iron and calcium. He prepared 2 sets of chef robot, a cook robot has N, each chef robot will only cook a dish, this dish can provide the first I kind of nutrition XI. If you want to eat this dish, just enter a number, you can eat the corresponding number of this dish. In order to prevent the injury caused by excessive ingestion, each robot also has an overdose of drugs, as long as the input of a number, can produce a certain dose of medicine, eat these drugs, can reduce the equivalent of the amount of food provided by this dish of nutrition.
Kobayashi prepared 2 sets of chef robot, it is because of the long journey, it is difficult to predict, perhaps a chef robot in the way of the bad, if the impact of the Galaxy player's body, it is not good. Therefore, the 2nd set of Chef Robots is used to make a 1th set of spare. Kobayashi needs for each of the 1th set of Chef robot to choose a 2nd set of chef robot for backup, so that when the robot is broken, with backup replacement, the entire chef robot can still match any nutritional needs, and, each 2nd set of Chef robot can only be a 1th set of chef robot backup.
Input

The first row contains a positive integer n. Next n rows, n integers per row, representing the first
1 sets of chef robots make every pound of food available for each of the nutrients. Then the next n rows, n integers per line, represent the 2nd set of cooks that the robot makes every pound of each nutrient provided. Output

The first line is a string, if the task can not be completed, output "NIE", otherwise lose
Out "TAK", and followed by n rows, line I represents the 1th set of robot backup is which 2nd set of robots. To avoid trouble, if there are multiple possible answers, give the group with the smallest dictionary order.

For each bi b_i to find out which vector AJ A_j He can replace, then it becomes a complete matching problem of binary graphs with minimal dictionary order.
The vector A A is composed of the matrix A, then the Xa=bi xa=b_i, that is Bi=x1a1+x2a2+⋯+xnan b_i=x_1a_1+x_2a_2+\cdots +x_na_n. If the XJ X_j is Non-zero, the bi b_i can replace AJ A_j. The B-B is spelled into the Matrix b b, and the C=b∗a−1 c=b*a^{-1} is the adjacency matrix of the binary graph.
A complete match is first obtained on the graph, then the edges of each position are considered from small to large, and a path that is minimal and that does not affect the previous match is selected for replacement.
The original title data does not seem to guarantee that matrix A is reversible and needs to be judged.

#include <cstdio> #include <cstring> #include <algorithm> using namespace std;
#define LL Long const int p=100000007;
int n,f[310],vis[310],ans[310],ok=1;
    int pow (int base,int x) {int ret=1;
        while (x) {if (x&1) ret= (LL) ret*base%p;
        Base= (LL) base*base%p;
    x>>=1;
return ret;
    } struct Mat {int a[310][310]; Void Rd () {for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) scanf ("%d", &a[
    I][J]);
        } MAT operator * (const mat &m) const {MAT ret;
                for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {ret.a[i][j]=0;
            for (int k=1;k<=n;k++) ret.a[i][j]= (ret.a[i][j]+ (LL) a[i][k]*m.a[k][j]%p)%p;
    return ret;
        } Mat Inv () {MAT ret; for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) ret.a[i][j]= (I==J);
            for (int i=1;i<=n;i++) {int x;
            X=-1;
                    for (int j=i;j<=n;j++) if (A[j][i]) {x=j;
                Break
                } if (x==-1) {ok=0;
            return ret; } if (x!=i) for (int j=1;j<=n;j++) {swap (a[i][j],a[x][
                    J]);
                Swap (ret.a[i][j],ret.a[x][j]);
            } x=pow (A[i][i],p-2);
                for (int j=1;j<=n;j++) {a[i][j]= (LL) a[i][j]*x%p;
            ret.a[i][j]= (LL) ret.a[i][j]*x%p;
                    for (int j=1;j<=n;j++) if (j!=i) {x=a[j][i]; for (int k=1;k<=n;k++) {a[j][k]= (a[j][k]-(LL) a[i][k]*x%p+p)%
                        P Ret. a[j][k]= (ret.a[j][k]-(LL) ret.a[i][k]*x%p+p)%p;
    }} return ret;
}}a,b,c;
            int dfs1 (int u) {for (int v=1;v<=n;v++) if (c.a[v][u]&&!vis[v)) {vis[v]=1; if (!f[v]| |
                DFS1 (F[v])) {f[v]=u;
            return 1;
} return 0; int dfs2 (int u,int from) {for (int v=1;v<=n;v++) if (C.a[v][u]&&!vis[v]) {V
            Is[v]=1; if (f[v]==from| |
            (F[V]&GT;FROM&AMP;&AMP;DFS2 (F[v],from))
                {f[v]=u;
            return 1;
} return 0;
    int main () {int cnt=0;
    scanf ("%d", &n);
    A.rd ();
    B.rd ();
    C=B*A.INV ();
        if (!ok) {printf ("nie\n");
    return 0;
        for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) vis[j]=0;
    if (DFS1 (i)) cnt++; } if (cnt<n) printf ("nie\n");
        else {printf ("tak\n");
            for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) vis[j]=0;
        DFS2 (I,i); for (  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.