Submission #581334

# Submission time Handle Problem Language Result Execution time Memory
581334 2022-06-22T13:31:05 Z yutabi Marriage questions (IZhO14_marriage) C++14
20 / 100
280 ms 7484 KB
#include <bits/stdc++.h>
using namespace std;

#define pb push_back

int n;
int m;

int k;


vector <vector <int> > l;

int ans;

vector <int> s;
vector <set <int> > fr;

vector <bool> flag;

int u;

vector <int> path;

bool DFS(int node)
{
    flag[node]=1;

    if(s[node]==-1)
    {
        path.pb(node);

        return 1;
    }

    for(int i=0;i<l[s[node]].size();i++)
    {
        if(flag[l[s[node]][i]]==0)
        {
            bool res=DFS(l[s[node]][i]);

            if(res==1)
            {
                path.pb(node);

                return 1;
            }
        }
    }

    return 0;
}

int main()
{
    scanf("%d %d %d",&n,&m,&k);

    s=vector <int> (m,-1);
    fr=vector <set <int> > (m);
    flag=vector <bool> (m);
    l=vector <vector <int> > (n);

    for(int i=0;i<k;i++)
    {
        int a,b;

        scanf(" %d %d",&a,&b);
        a--,b--;

        l[a].pb(b);
    }

    int ptr1=0;
    int ptr2=0;

    while(1)
    {
        if(u<m)
        {
            if(ptr2>=n)
            {
                break;
            }

            for(int i=0;i<l[ptr2].size();i++)
            {
                fr[l[ptr2][i]].insert(ptr2); 
            }



            ptr2++;
        }

        else
        {
            ans+=n-ptr2+1;

            int y=-1;

            for(int i=0;i<l[ptr1].size();i++)
            {
                if(s[l[ptr1][i]]==ptr1)
                {
                    u--;

                    s[l[ptr1][i]]=-1;
                }
            }

            if(y==-1)
            {
                for(int i=0;i<l[ptr1].size();i++)
                {
                    fr[l[ptr1][i]].erase(ptr1);
                }
            }

            ptr1++;
        }

        for(int i=0;i<m;i++)
        {
            while(flag[i]==0 && fr[i].size()>0)
            {
                path.clear();

                if(DFS(i))
                {
                    u++;

                    /*if(ptr2==2)
                    {
                        printf("\n\n");

                        for(int j=0;j<path.size();j++)
                        {
                            printf("%d ",path[j]);
                        }

                        printf("\n\n%d\n",s[0]);
                    }*/

                    for(int j=path.size()-1;j>0;j--)
                    {
                        flag[path[j]]=0;

                        s[path[j]]=s[path[j-1]];
                    }

                    flag[i]=0;

                    int nw=*(fr[i].begin());

                    for(int j=0;j<l[nw].size();j++)
                    {
                        fr[l[nw][j]].erase(nw);
                    }

                    s[i]=nw;
                }
            }
        }
        /*printf("\n%d %d %d\n",ptr1,ptr2,u);

        for(int i=0;i<m;i++)
        {
            printf("%d ",s[i]);
        }

        printf("\n");*/
    }

    printf("%d",ans);
}

Compilation message

marriage.cpp: In function 'bool DFS(int)':
marriage.cpp:36:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   36 |     for(int i=0;i<l[s[node]].size();i++)
      |                 ~^~~~~~~~~~~~~~~~~~
marriage.cpp: In function 'int main()':
marriage.cpp:85:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |             for(int i=0;i<l[ptr2].size();i++)
      |                         ~^~~~~~~~~~~~~~~
marriage.cpp:101:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |             for(int i=0;i<l[ptr1].size();i++)
      |                         ~^~~~~~~~~~~~~~~
marriage.cpp:113:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  113 |                 for(int i=0;i<l[ptr1].size();i++)
      |                             ~^~~~~~~~~~~~~~~
marriage.cpp:155:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  155 |                     for(int j=0;j<l[nw].size();j++)
      |                                 ~^~~~~~~~~~~~~
marriage.cpp:56:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   56 |     scanf("%d %d %d",&n,&m,&k);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
marriage.cpp:67:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   67 |         scanf(" %d %d",&a,&b);
      |         ~~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 1 ms 212 KB Output isn't correct
2 Correct 1 ms 304 KB Output is correct
3 Incorrect 1 ms 212 KB Output isn't correct
4 Incorrect 1 ms 288 KB Output isn't correct
5 Correct 1 ms 300 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Incorrect 1 ms 212 KB Output isn't correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 304 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Incorrect 1 ms 212 KB Output isn't correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Incorrect 1 ms 212 KB Output isn't correct
15 Incorrect 1 ms 212 KB Output isn't correct
16 Incorrect 1 ms 212 KB Output isn't correct
17 Incorrect 1 ms 304 KB Output isn't correct
18 Incorrect 1 ms 212 KB Output isn't correct
19 Incorrect 1 ms 312 KB Output isn't correct
20 Incorrect 1 ms 300 KB Output isn't correct
21 Incorrect 1 ms 212 KB Output isn't correct
22 Incorrect 1 ms 212 KB Output isn't correct
23 Incorrect 1 ms 212 KB Output isn't correct
24 Incorrect 1 ms 212 KB Output isn't correct
25 Incorrect 4 ms 468 KB Output isn't correct
26 Incorrect 1 ms 340 KB Output isn't correct
27 Correct 1 ms 340 KB Output is correct
28 Incorrect 1 ms 212 KB Output isn't correct
29 Incorrect 4 ms 724 KB Output isn't correct
30 Incorrect 3 ms 724 KB Output isn't correct
31 Incorrect 17 ms 980 KB Output isn't correct
32 Incorrect 6 ms 468 KB Output isn't correct
33 Correct 3 ms 296 KB Output is correct
34 Incorrect 3 ms 340 KB Output isn't correct
35 Incorrect 27 ms 2552 KB Output isn't correct
36 Incorrect 20 ms 2048 KB Output isn't correct
37 Incorrect 36 ms 1208 KB Output isn't correct
38 Incorrect 34 ms 3532 KB Output isn't correct
39 Incorrect 87 ms 732 KB Output isn't correct
40 Incorrect 117 ms 1184 KB Output isn't correct
41 Incorrect 93 ms 1688 KB Output isn't correct
42 Incorrect 56 ms 2636 KB Output isn't correct
43 Incorrect 67 ms 3644 KB Output isn't correct
44 Incorrect 127 ms 5808 KB Output isn't correct
45 Incorrect 232 ms 3220 KB Output isn't correct
46 Incorrect 250 ms 6872 KB Output isn't correct
47 Incorrect 238 ms 7284 KB Output isn't correct
48 Incorrect 212 ms 6608 KB Output isn't correct
49 Incorrect 264 ms 7484 KB Output isn't correct
50 Incorrect 280 ms 1200 KB Output isn't correct